Topics

Python Logging Classes and Functions

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.

 

Few classes and functions in the logging module.

Below are the commonly used classes that are defined in the logging module already:

  • Logger class
    The Logger is typically the class whose objects will be used in the application code to call the functions directly.
  •  LogRecord
    The Logger class automatically creates LogRecord objects that contain the information related to the event being logged (or the log message). The LogRecord also contains information such as the name of the logger, the function, the line number, the message, the process id, and more.
  • Handler
    These are typically used to send the LogRecord to the required output destination, and it could either be a console or a file. The class Handler can be regarded as the base for subclasses such as the StreamHandler, FileHandler, SMTPHandler, HTTPHandler, and so on. These subclasses send the logging outputs to corresponding destinations such as the disk file or the sys.stdout.
  •  Formatter
    In Python, the Formatter is a class in which we can specify the format of the output by specifying a string format that lists out the attributes the output would contain mainly.

Several Logger objects

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.

ObjectDescription
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.