Welcome to a tutorial on Logging Classes and Functions in Python.
Previously, you were introduced to how to use and configure the default logger named root, which is mainly used by the logging module whenever its function is called directly, like this: logging.info().
Interestingly, in Python, one can define its logger by just creating an object of the Logger class, and this is very important for multiple module applications.
Below are the commonly used classes that are defined in the logging module already:
In Logging Module, objects or items with all caps are constant, the capitalize items indicate classes, while the items that start with lowercase letters are methods. The table below consists of several logger objects offered by the logging module.
Object | Description |
Logger.info(msg) | The function typically helps to log a message with level INFO on this logger. |
Logger.warning(msg) | This function typically helps to log a message with a level WARNING on this logger. |
Logger.error(msg) | This function typically helps to log a message with level ERROR on this logger. |
Logger.critical(msg) | This function typically helps to log a message with level CRITICAL on this logger. |
Logger.setLevel(lvl) | This function is used to set the threshold of the logger to lvl and that means that all the messages below this level will be ignored. |
Logger.exception(msg) | The function helps to log a message with level ERROR on this logger. |
Logger.log(lvl, msg) | This function will Logs a message with integer level lvl on this logger. |
Logger.filter(record) | This method is used to apply the logger's filter to the provided record and thus returns True if the record is to be processed. else, it returns False. |
Logger.addFilter(filt) | This is used to add a specific filter file to this logger. |
Logger.removeFilter(filt) | This is used to add a specific filter file from this logger. |
Logger.hasHandlers() | It is used to check if the logger has any handler configured or not. |
Logger.addHandler(hdlr) | This is used to add a specific handler hdlr to this logger. |
Logger.removeHandler(hdlr) | It is used to remove a specific handler hdlr to this logger. |