Debian Codegraph @

docs_sphinx/advanced/logging.rst

raw
1 Logging
2 =======
3
4 Brian uses a logging system to display warnings and general information messages
5 to the user, as well as writing them to a file with more detailed information,
6 useful for debugging. Each log message has one of the following "log levels":
7
8 ``ERROR``
9 Only used when an exception is raised, i.e. an error occurs and the current
10 operation is interrupted. *Example:* You use a variable name in an equation
11 that Brian does not recognize.
12
13 ``WARNING``
14 Brian thinks that something is most likely a bug, but it cannot be sure.
15 *Example:* You use a `Synapses` object without any synapses in your
16 simulation.
17
18 ``INFO``
19 Brian wants to make the user aware of some automatic choice that it did for
20 the user. *Example:* You did not specify an integration ``method`` for a
21 `NeuronGroup` and therefore Brian chose an appropriate method for you.
22
23 ``DEBUG``
24 Additional information that might be useful when a simulation is not working
25 as expected. *Example:* The integration timestep used during the simulation.
26
27 ``DIAGNOSTIC``
28 Additional information useful when tracking down bugs in Brian itself.
29 *Example:* The generated code for a `CodeObject`.
30
31 By default, all messages with level ``DEBUG`` or above are written to the log file
32 and all messages of level ``INFO`` and above are displayed on the console. To change
33 what messages are displayed, see below.
34
35 .. note:: By default, the log file is deleted after a successful simulation run,
36 i.e. when the simulation exited without an error. To keep the log around,
37 set the `logging.delete_log_on_exit` preference to ``False``.
38
39 .. _logging_and_multiprocessing:
40
41 Logging and multiprocessing
42 ---------------------------
43 Brian's logging system is not designed for multiple parallel Brian processes
44 started via Python's `multiprocessing` module (see the
45 :ref:`multiprocessing examples <multiprocessing>`). Log messages that get printed
46 from different processes to the console are not printed in a well-defined order and
47 do not contain any indication about which processes they are coming from. You might
48 therefore consider using e.g. `BrianLogger.log_level_error()` to only show error
49 messages before starting the processes and avoid cluttering your console with
50 warning and info messages.
51
52 To avoid issues when multiple processes try to log to the same log file, file
53 logging is automatically switched off for all processes except for the initial
54 process. If you need a file log for sub-processes, you can call
55 `BrianLogger.initialize()` in each sub-process. This way, each process will log
56 to its own file.
57
58 Showing/hiding log messages
59 ---------------------------
60 If you want to change what messages are displayed on the console, you can call a
61 method of the method of `BrianLogger`::
62
63 BrianLogger.log_level_debug() # now also display debug messages
64
65 It is also possible to suppress messages for certain sub-hierarchies by using
66 `BrianLogger.suppress_hierarchy`::
67
68 # Suppress code generation messages on the console
69 BrianLogger.suppress_hierarchy('brian2.codegen')
70 # Suppress preference messages even in the log file
71 BrianLogger.suppress_hierarchy('brian2.core.preferences',
72 filter_log_file=True)
73
74 Similarly, messages ending in a certain name can be suppressed with
75 `BrianLogger.suppress_name`::
76
77 # Suppress resolution conflict warnings
78 BrianLogger.suppress_name('resolution_conflict')
79
80 These functions should be used with care, as they suppresses messages
81 independent of the level, i.e. even warning and error messages.
82
83 Preferences
84 -----------
85 You can also change details of the logging system via Brian's :doc:`preferences`
86 system. With this mechanism, you can switch the logging to a file off completely
87 (by setting `logging.file_log` to ``False``) or have it log less messages (by
88 setting `logging.file_log_level` to a level higher than ``DEBUG``). To debug
89 details of the code generation system, you can also set `logging.file_log_level`
90 to ``DIAGNOSTIC``. Note that this will make the log file grow quickly in size. To
91 prevent it from filling up the disk, it will only be allowed to grow up to a certain
92 size. You can configure the maximum file size with the `logging.file_log_max_size`
93 preference.
94
95 For a list of all preferences related to logging, see the documentation of the
96 `brian2.utils.logger` module.
97
98 .. warning:: Most of the logging preferences are only taken into account during
99 the initialization of the logging system which takes place as soon as `brian2`
100 is imported. Therefore, if you use e.g. `prefs.logging.file_log = False` in
101 your script, this will not have the intended effect! To make sure these
102 preferences are taken into account, call `BrianLogger.initialize` after
103 setting the preferences. Alternatively, you can set the preferences in a file
104 (see :doc:`preferences`).