Skip to content

Latest commit

 

History

History

logging

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

logging

This module provides a polyfill for the logging module (introduced in Python 2.3).

Features

  • Initialization possible via getLogger function (as in the original module) or Logger class
  • Logs printed only if level is enabled (e.g. logger.debug(...) will not print anything if level is set to INFO)
  • Logger can have custom names (default is root)

Usage

Initialization via basicConfig(...)

This method will allow you to initialize the root logger with a single call. To use the root logger just call the log methods on the logging module itself:

import polyfills.logging as logging

# NOTE: All parameters are optional
logging.basicConfig(
    level=logging.INFO,
    format="[%(asctime)s] %(levelname)s - %(name)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S"
)

logging.debug("This will not be printed")
logging.info("This will be printed")

Initialization via getLogger(...)

This method will allow you to initialize a custom logger. To use the custom logger just call the log methods on the newly created logger:

import polyfills.logging as logging

logger = logging.getLogger("my_logger")
logger.setLevel(logging.INFO)        # Set level to 'INFO' (default is 'NOTSET')

logger.debug("This will not be printed")
logger.info("This will be printed")

WARNING:

At the moment, since the Formatter class is still not implemented if you use the getLogger method, you will not be able to change the format of the logs.

Currently, the only supported way to do this is to use the basicConfig function.

A workaround would be to use the internal properties logger._format and logger._time_format to change the format of the logs:

logger = logging.getLogger("my_logger")
logger._format = "[%(asctime)s] %(levelname)s - %(name)s - %(message)s"

Keep in mind that things might change in the future, so use this workaround at your own risk!

⚠️ Known limitations & gotchas

  • To keep things simple, this module is NOT thread-safe (this will make the difference ONLY if you need to write logs from multiple threads).
  • The basicConfig function is implemented but some of its features are missing:
    • filename/filemode/encoding/errors: Files are not supported (logs are always printed to stdout)
    • style: Currently only the default style is supported (same as style='%')
    • stream: Streams are not supported
    • handlers: Custom handlers are not supported
    • force: Currently every call to the basicConfig function overwrites the previous configuration (same as force=True)
  • The Logger object...
    • cannot have custom filters or handlers (no addFilter or addHandler methods)