Uncertainpy uses the logging module to log to both file and to screen. All loggers are named class_instance.__module__ + "." +  class_instance.__class__.__name__. An example, the logger in a Data```object is named ``uncertainpy.data.Data. If the the module name does not start with “uncertainpy.”, “uncertainpy.” as added as a prefix.

A file handler is only added to the logging by UncertaintyQuantification. If level is set to None, no logging in Uncertainpy is set up and the logging can be customized as necessary by using the logging module. This should only be done if you know what you are doing. Be warned that logging is performed in parallel. If the MultiprocessLoggingHandler() is not used when trying to write to a single log file, Uncertainpy will hang. This happens because several processes try to log to the same file.

Logging can easily be added to custom models and features by:

# Import the functions and libraries needed
from uncertainpy.utils import create_logger
import logging

# Set up a logger. This adds a screen handlers to the "uncertainpy" logger
# if it does not already exist
# All log messages with level "info" or higher will be logged.
setup_logger("uncertainpy.logger_name", level="info")

# Get the logger recently created
logger = logging.getLogger("uncertainpy.logger_name")

# Log a message with the level "info".
logger.info("info logging message here")

Note that if you want to use the logger setup in Uncertainpy, the name of your loggers should start with uncertainpy..

API Reference

class uncertainpy.utils.logger.MultiprocessLoggingHandler(filename, mode)[source]

Adapted from: https://stackoverflow.com/questions/641420/how-should-i-log-while-using-multiprocessing-in-python


Tidy up any resources used by the handler.

This version removes the handler from an internal map of handlers, _handlers, which is used for handler lookup by name. Subclasses should ensure that this gets called from overridden close() methods.


Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.


Set the formatter for this handler.

class uncertainpy.utils.logger.MyFormatter(fmt=u'%(levelno)s: %(msg)s')[source]

The logging formater.


Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

class uncertainpy.utils.logger.TqdmLoggingHandler(stream=None)[source]

Set logging so logging to stream works with Tqdm, logging now uses tqdm.write.


Emit a record.

If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newline. If exception information is present, it is formatted using traceback.print_exception and appended to the stream. If the stream has an ‘encoding’ attribute, it is used to determine how to do the output to the stream.

uncertainpy.utils.logger.add_file_handler(name=u'uncertainpy', filename=u'uncertainpy.log')[source]

Add file handler to logger with name, if no file handler already exists for the given logger.

  • name (str, optional) – Name of the logger. Default name is “uncertainpy”.
  • filename (str) – Name of the logfile. If None, no logging to file is performed. Default is “uncertainpy.log”.

Adds a logging to console (a console handler) to logger with name, if no screen handler already exists for the given logger.

Parameters:name (str, optional) – Name of the logger. Default name is “uncertainpy”.

Get a logger with name given from class_instance: class_instance.__module__ + "." +  class_instance.__class__.__name__.

Parameters:class_instance (instance) – Class instance used to get the logger name.
Returns:logger – The logger object.
Return type:Logger object

See if this logger has any handlers configured.

Loop through all handlers for this logger and its parents in the logger hierarchy. Return True if a handler was found, else False. Stop searching up the hierarchy whenever a logger with the “propagate” attribute set to zero is found - that will be the last logger which is checked for the existence of handlers.

Returns:True if the logger or any parent logger has handlers attached.
Return type:bool
uncertainpy.utils.logger.setup_logger(name, level=u'info')[source]

Create a logger with name.

  • name (str) – Name of the logger
  • level ({“info”, “debug”, “warning”, “error”, “critical”, None}, optional) – Set the threshold for the logging level. Logging messages less severe than this level is ignored. If None, no logger is set up. Default logger level is info.
uncertainpy.utils.logger.setup_module_logger(class_instance, level=u'info')[source]

Create a logger with a name from the current class. “uncertainpy.” is added to the beginning of the name if the module name does not start with “uncertainpy.”. If no handlers, adds handlers to the logger named uncertainpy.

  • class_instance (instance) – Class instance used to set the logger name. class_instance.__module__ + "." +  class_instance.__class__.__name__.
  • level ({“info”, “debug”, “warning”, “error”, “critical”, None}, optional) – Set the threshold for the logging level. Logging messages less severe than this level is ignored. If None, no logger level is set. Setting logger level overwrites the logger level set from configuration file. Default logger level is “info”.