This module provides a polyfill for the logging
module (introduced in Python 2.3).
- Initialization possible via
getLogger
function (as in the original module) orLogger
class - 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
Formatter
class is still not implemented if you use thegetLogger
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
andlogger._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!
- 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 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 thebasicConfig
function overwrites the previous configuration (same asforce=True
)
- The
Logger
object...- cannot have custom
filters
orhandlers
(noaddFilter
oraddHandler
methods)
- cannot have custom