LogHelper#
- class log_helpers.LogHelper(root_logger, tool_details, console=True, log_file=None, warning_capture=True)#
Bases:
objectClass for managing Python loggers.
- Parameters:
root_logger (str) – Name of the root logger to add handlers to, should be the name of the Python package.
tool_details (ToolDetails) – Details of the tool being ran.
console (bool, optional) – If True (default) output log messages to the console with default settings.
log_file (os.PathLike, optional) – If given output log messages to a file with default settings.
warning_capture (bool, optional) – If True (default) capture, and log, Python warnings.
Examples
When using Python’s built-in logging functionality a module level logger constant should be used.
>>> import logging >>> >>> LOG = logging.getLogger(__name__)
This module constant should be used for logging any messages, in one of 5 levels.
>>> LOG.debug("Log a debug message") >>> LOG.info("Log an info message") >>> LOG.warning("Log a warning message") >>> LOG.error("Log an error message") >>> LOG.critical("Log a critical message")
To determine where log messages are written to (console / log file) the log handlers need to be setup, the LogHelper class can do this and will automatically clean-up upon exiting using a with statement.
The example below shows how to setup logging with a log file using the LogHelper class, which will create a log file and write system and tool information to it automatically.
>>> # Temp directory for testing purposes >>> tmp_path = getfixture('tmp_path') >>> path = tmp_path / "test.log" >>> details = ToolDetails("test", "1.2.3") >>> >>> with LogHelper(__package__, details, log_file=path): ... # Add main function for running your tool here ... ... # Any log messages within the with statement will be written to ... # the log file, even if running in other functions / modules ... LOG.info("Log messages using module logger")
The following example shows how to setup logging with a custom console or file output, this also allows log files to be added after initial setup of LogHelper e.g. in another function after the output directory is known.
>>> with LogHelper(__package__, details, console=False) as log_helper: ... # Console handler with custom message format ... log_helper.add_console_handler(ch_format="[%(levelname)-8.8s] %(message)s") ... # File handler with custom message format ... log_helper.add_file_handler(path, fh_format="[%(levelname)-8.8s] %(message)s") ... ... # Write initialisation log message with system and tool information ... log_helper.write_instantiate_message()
Methods Summary
add_console_handler([ch_format, ...])Add custom console handler to the logger.
add_file_handler(log_file[, fh_format, ...])Add custom file handler to the logger.
add_handler(handler)Add custom handler to the logger.
Capture warnings using logging.
Flush and close all handlers before removing from the logger.
Log instatiation message with tool and system information.
Methods Documentation
- add_console_handler(ch_format='[%(asctime)s - %(levelname)-8.8s] %(message)s', datetime_format='%H:%M:%S', log_level=20)#
Add custom console handler to the logger.
- Parameters:
ch_format (str) – A string defining a custom formatting to use for the StreamHandler(). Defaults to “[%(levelname)-8.8s] %(message)s”.
datetime_format (str) – The datetime format to use when logging to the console. Defaults to “%H:%M:%S”
log_level (int) – The logging level to give to the StreamHandler.
- Return type:
None
See also
None
- add_file_handler(log_file, fh_format='%(asctime)s [%(name)-40.40s] [%(levelname)-8.8s] %(message)s', datetime_format='%d-%m-%Y %H:%M:%S', log_level=10)#
Add custom file handler to the logger.
- Parameters:
log_file (PathLike) – The path to a file to output the log
fh_format (str) – A string defining a custom formatting to use for the StreamHandler(). Defaults to “%(asctime)s [%(name)-40.40s] [%(levelname)-8.8s] %(message)s”.
datetime_format (str) – The datetime format to use when logging to the console. Defaults to “%d-%m-%Y %H:%M:%S”
log_level (int) – The logging level to give to the FileHandler.
- Return type:
None
See also
None
- add_handler(handler)#
Add custom handler to the logger.
This will also add handler to the warnings logger if warnings capture is enabled.
- Parameters:
handler (logging.Handler) – Handler to add.
- Return type:
None
- capture_warnings()#
Capture warnings using logging.
Runs logging.captureWarnings(True) to capture warnings then adds all the handlers from the root logger.
See also
None- Return type:
None
- cleanup_handlers()#
Flush and close all handlers before removing from the logger.
Cleans up logger and warnings logger (if exists).
- Return type:
None
- write_instantiate_message()#
Log instatiation message with tool and system information.
- Return type:
None