Debian Codegraph @

docs_sphinx/developer/guidelines/logging.rst

raw
1 .. currentmodule:: brian2
2
3 Logging
4 =======
5
6 For a description of logging from the users point of view, see :doc:`../../advanced/logging`.
7
8 Logging in Brian is based on the :mod:`logging` module in Python's standard
9 library.
10
11 Every brian module that needs logging should start with the following line,
12 using the `get_logger` function to get an instance of `BrianLogger`::
13
14 logger = get_logger(__name__)
15
16 In the code, logging can then be done via::
17
18 logger.diagnostic('A diagnostic message')
19 logger.debug('A debug message')
20 logger.info('An info message')
21 logger.warn('A warning message')
22 logger.error('An error message')
23
24 If a module logs similar messages in different places or if it might be useful
25 to be able to suppress a subset of messages in a module, add an additional
26 specifier to the logging command, specifying the class or function name, or
27 a method name including the class name (do not include the module name, it will
28 be automatically added as a prefix)::
29
30 logger.debug('A debug message', 'CodeString')
31 logger.debug('A debug message', 'NeuronGroup.update')
32 logger.debug('A debug message', 'reinit')
33
34 If you want to log a message only once, e.g. in a function that is called
35 repeatedly, set the optional ``once`` keyword to ``True``::
36
37 logger.debug('Will only be shown once', once=True)
38 logger.debug('Will only be shown once', once=True)
39
40 The output of debugging looks like this in the log file::
41
42 2012-10-02 14:41:41,484 DEBUG brian2.equations.equations.CodeString: A debug message
43
44 and like this on the console (if the log level is set to "debug")::
45
46 DEBUG A debug message [brian2.equations.equations.CodeString]
47
48 .. _log_level_recommendations:
49
50 Log level recommendations
51 -------------------------
52 diagnostic
53 Low-level messages that are not of any interest to the normal user but
54 useful for debugging Brian itself. A typical example is the source
55 code generated by the code generation module.
56 debug
57 Messages that are possibly helpful for debugging the user's code. For example,
58 this shows which objects were included in the network, which clocks the
59 network uses and when simulations start and stop.
60
61 info
62 Messages which are not strictly necessary, but are potentially helpful for
63 the user. In particular, this will show messages about the chosen state
64 updater and other information that might help the user to achieve better
65 performance and/or accuracy in the simulations (e.g. using ``(event-driven)``
66 in synaptic equations, avoiding incompatible ``dt`` values between
67 `TimedArray` and the `NeuronGroup` using it, ...)
68 warn
69 Messages that alert the user to a potential mistake in the code, e.g. two
70 possible resolutions for an identifier in an equation. In such cases, the
71 warning message should include clear information how to change the code
72 to make the situation unambigous and therefore make the warning message
73 disappear. It can also be used to make the user aware that he/she is using
74 an experimental feature, an unsupported compiler or similar. In this case,
75 normally the ``once=True`` option should be used to raise this warning only
76 once. As a rule of thumb, "common" scripts like the examples provided in
77 the examples folder should normally not lead to any warnings.
78 error
79 This log level is not used currently in Brian, an exception should be
80 raised instead. It might be useful in "meta-code", running scripts and
81 catching any errors that occur.
82
83 The default log level shown to the user is ``info``. As a general rule, all
84 messages that the user sees in the default configuration (i.e., ``info`` and
85 ``warn`` level) should be avoidable by simple changes in the user code, e.g.
86 the renaming of variables, explicitly specifying a state updater instead of
87 relying on the automatic system, adding ``(clock-driven)``/``(event-driven)``
88 to synaptic equations, etc.
89
90 Testing log messages
91 --------------------
92 It is possible to test whether code emits an expected log message using the
93 `~brian2.utils.logger.catch_logs` context manager. This is normally not
94 necessary for debug and info messages, but should be part of the unit tests
95 for warning messages (`~brian2.utils.logger.catch_logs` by default only catches
96 warning and error messages)::
97
98 with catch_logs() as logs:
99 # code that is expected to trigger a warning
100 # ...
101 assert len(logs) == 1
102 # logs contains tuples of (log level, name, message)
103 assert logs[0][0] == 'WARNING' and logs[0][1].endswith('warning_type')
104
105 Logging in extension packages
106 -----------------------------
107 Extension packages such as `brian2cuda <https://brian2cuda.readthedocs.io>`_ can use Brian's logging infrastructure by
108 using the `~brian2.utils.logger.get_logger` function to get a logger instance. They should use their own module name,
109 e.g. a name starting with ``brian2cuda.`` so that it is clear whether a log message comes from Brian or from an
110 extension package. This is also used by the `~.catch_logs` context manager (see above) to only consider log messages
111 from the ``brian2`` package.