This module provides a polyfill for the logging module (introduced in Python 2.3).
- Initialization possible via
getLoggerfunction (as in the original module) orLoggerclass - Logs printed only if level is enabled (e.g.
logger.debug(...)will not print anything if level is set toINFO) - Logger can have custom names (default is
root)
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")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
Formatterclass is still not implemented if you use thegetLoggermethod, you will not be able to change the format of the logs.Currently, the only supported way to do this is to use the
basicConfigfunction.A workaround would be to use the internal properties
logger._formatandlogger._time_formatto 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!
- 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
basicConfigfunction is implemented but some of its features are missing:filename/filemode/encoding/errors: Files are not supported (logs are always printed tostdout)style: Currently only the default style is supported (same asstyle='%')stream: Streams are not supportedhandlers: Custom handlers are not supportedforce: Currently every call to thebasicConfigfunction overwrites the previous configuration (same asforce=True)
- The
Loggerobject...- cannot have custom
filtersorhandlers(noaddFilteroraddHandlermethods)
- cannot have custom