For a description of logging from the users point of view, see Logging.
Logging in Brian is based on the
logging module in Python’s standard
logger = get_logger(__name__)
In the code, logging can then be done via:
logger.diagnostic('A diagnostic message') logger.debug('A debug message') logger.info('An info message') logger.warn('A warning message') logger.error('An error message')
If a module logs similar messages in different places or if it might be useful to be able to suppress a subset of messages in a module, add an additional specifier to the logging command, specifying the class or function name, or a method name including the class name (do not include the module name, it will be automatically added as a prefix):
logger.debug('A debug message', 'CodeString') logger.debug('A debug message', 'NeuronGroup.update') logger.debug('A debug message', 'reinit')
If you want to log a message only once, e.g. in a function that is called
repeatedly, set the optional
once keyword to
logger.debug('Will only be shown once', once=True) logger.debug('Will only be shown once', once=True)
The output of debugging looks like this in the log file:
2012-10-02 14:41:41,484 DEBUG brian2.equations.equations.CodeString: A debug message
and like this on the console (if the log level is set to “debug”):
DEBUG A debug message [brian2.equations.equations.CodeString]
Log level recommendations¶
- Low-level messages that are not of any interest to the normal user but useful for debugging Brian itself. A typical example is the source code generated by the code generation module.
- Messages that are possibly helpful for debugging the user’s code. For example, this shows which objects were included in the network, which clocks the network uses and when simulations start and stop.
- Messages which are not strictly necessary, but are potentially helpful for
the user. In particular, this will show messages about the chosen state
updater and other information that might help the user to achieve better
performance and/or accuracy in the simulations (e.g. using
(event-driven)in synaptic equations, avoiding incompatible
NeuronGroupusing it, ...)
- Messages that alert the user to a potential mistake in the code, e.g. two
possible solutions for an identifier in an equation. It can also be used to
make the user aware that he/she is using an experimental feature, an
unsupported compiler or similar. In this case, normally the
once=Trueoption should be used to raise this warning only once. As a rule of thumb, “common” scripts like the examples provided in the examples folder should normally not lead to any warnings.
- This log level is not used currently in Brian, an exception should be raised instead. It might be useful in “meta-code”, running scripts and catching any errors that occur.
The default log level shown to the user is
info. As a general rule, all
messages that the user sees in the default configuration (i.e.,
warn level) should be avoidable by simple changes in the user code, e.g.
the renaming of variables, explicitly specifying a state updater instead of
relying on the automatic system, adding
to synaptic equations, etc.
Testing log messages¶
It is possible to test whether code emits an expected log message using the
catch_logs context manager. This is normally not
necessary for debug and info messages, but should be part of the unit tests
for warning messages (
catch_logs by default only catches
warning and error messages):
with catch_logs() as logs: # code that is expected to trigger a warning # ... assert len(logs) == 1 # logs contains tuples of (log level, name, message) assert logs == 'WARNING' and logs.endswith('warning_type')