dfds_ds_toolbox.logging.logging module
- dfds_ds_toolbox.logging.logging.init_logger(name=None, stream_level='WARNING', debug_file=None, rich_handler_enabled=True, log_format=None)
Initialize a logger.
Set up logging that print to stdout with
stream_level(default value is WARNING). Ifdebug_fileis given set up logging to file with DEBUG level.- Parameters
name (
Optional[str,None]) – name of the loggerstream_level (
str) – threshold level for the loggingdebug_file (
Union[Path,str,None]) – filename for the logging debug filerich_handler_enabled (
bool) – enable rich handler with coloured textlog_format (
Optional[str,None]) – custom log format
- Return type
Logger- Returns
The Logger object.
Examples
Initializing the logger:
>>> logger = init_logger() >>> logger.info("This message will not be logged.") >>> logger.critical("Something BAD happened.")
If you want to log the messages on a different level than DEBUG (default) use:
>>> logger = init_logger(stream_level="INFO") >>> logger.debug("This message will not be logged.") >>> logger.info("Starting some work.")
If you want to save additionally the log the messages into a file use:
>>> logger = init_logger(stream_level="INFO", debug_file="path/log_files.log") >>> logger.debug("Logging something to a file.") >>> logger.info("Logging something to both terminal and file.")
If you do not want to use coloured text in the log use: >>> logger = init_logger(stream_level=”INFO”, rich_handler_enabled=False) >>> logger.info(“Not coloured text.”)
If you want to use a custom log format use: >>> logger = init_logger(stream_level=”INFO”, log_format=”%(module)s - %(message)s”, rich_handler_enabled=False) >>> logger.info(“Custom log format.”)