Logging in Feyn
by: Kevin Broløs
(Feyn version 3.1.0 or newer)
Feyn
uses the standard Python
logging
module for all messages. This means that the output can be configured and controlled as you usually would in a Python
application.
This allows you to suppress log outputs, or to choose logging messages to a file instead.
Any existing log configurations will always be honored.
For more information on the logging
module, we refer you to the official documentation and the official logging HOWTO.
Default configuration
If nothing else is set, the default log level for Feyn
is INFO.
Additionally, in the case where there is no existing configuration and the root logger
has no handlers, Feyn
will register its own StreamHandler
on the feyn
logger.
This is done primarily to support data science workflows and IPython
environments like Jupyter
, where users typically don't preconfigure log handlers.
Changing log levels
You can change the default log level using setLevel
, for instance:
import logging
logging.getLogger('feyn').setLevel("ERROR")
Disabling default behaviour
If you want to avoid the default behaviour, make sure to configure your logging before importing Feyn
.
A basic example that logs to a file:
import logging
logging.basicConfig(level=logging.INFO)
import feyn
Alternatively, if your own logger is configured after importing Feyn
, you can remove the handlers to avoid potentially handling logs on both the feyn
and root
level.
Example:
import feyn
import logging
logging.basicConfig()
feyn_logger = logging.getLogger('feyn')
for handler in feyn_logger.handlers:
feyn_logger.removeHandler(handler)
JupyterLogger
When Feyn
detects that it's running in IPython
environments like Jupyter
and if nothing else is configured, it configures the logging to use an extended Logger
class to ensure a nice default output for humans.
This logger uses the IPython.display
functionality for DEBUG and INFO level messages, and the logging system only for WARNING, ERROR and FATAL.
As mentioned above, if you want to avoid this behaviour, make sure to configure your logging before importing feyn
, or to set your own custom logger.
Disable display behavior
If you want to disable the IPython.display
bypass behavior and use the logging system instead, you can set use_display
to False
individually like in the example below:
logging.getLogger('feyn').use_display = False
logging.getLogger('feyn._model').use_display = False
# etc. (...)
or for all of them by setting it globally on the class property:
feyn._logging.JupyterLogger.use_display = False
Removing the logger class
If you have imported Feyn
prior to adding configuration, the JupyterLogger
is added using logging.setLoggerClass
and can be reverted by calling:
logging.setLoggerClass(logging.Logger)