Instantiate a logger

The module contains a function init_logger that returns a logger from the logging package with a fixed formatting, but choice in the log file name. The default name contains the date and time of execution.

A different log file is created in the given folder at each code run, if the default name for the log file is used. If you set an equal name from one run to another, the various logs will be appended to the same log file.

pyux comes with a default format for the logger, but you can specify your own logging.conf. Feel free to use ColourPen to color logger messages :

from pyux.logging import init_logger
from pyux.console import ColourPen
from shutil import rmtree

logger = init_logger(folder = 'logs', filename = 'activity', run_name = 'exemple', time_format = '%Y%m%d')
pen = ColourPen

# writes in green for debug
pen.write(color = 'green')
logger.debug('This ia a debug')

# writes in red for critical
pen.write(color = 'red', style = 'bright')
logger.critical('This is a critical')

# go back to normal for info
pen.write(style = 'RESET_ALL')
logger.info('This is an info')

# rmtree('logs')

The same logger can be used throughout a whole project by calling logger = logging.getLogger(__name__) in submodules of the main script.

Detailed documentation

pyux.logging.init_logger(folder: str = './logs', filename: str = 'activity', run_name: str = 'default', time_format: str = '%Y%m%d-%Hh%Mm%Ss', config_file=None)

Return a logger instance with predefined or custom format.

A different log file is saved at each execution. The logger is formatted with a default logging.conf when config_file is not provided.

Log file name is of the form filename_run_name_time-format. Default values yield, for instance : logs/activity_default_20190721-18h34h20s.log. You can cheat with time_format by specifying a normal word.

To use the logger across submodules, it is advised to instantiate the logger in the main script with logger = init_logger(), then instantiate it in the other modules with logger = logging.getLogger(__name__). This will not duplicate logging instances, and will display in the logged message the name of the module from which the logger was called.

  • folder (str) – default './logs' : folder to save log files in, created if does not exist
  • filename (str) – default 'activity' : name of log files
  • run_name – default 'default' : name of run
  • time_format (str) – default '%Y%m%d-%Hh%Mm%Ss' : time format for date
  • config_file (str) – default None : path to logging.conf file

run_name: str


A ‘root’ logger from logging package

>>> logger = init_logger(
>>>     folder = 'logs', filename = 'exemple',
>>>     run_name = 'daily-run', time_format = "%Y%m%d"
>>> )