Logging Classes and Functions

Logging Classes & Functions

So far, we have seen how we can create logs as well as how to write logs to a file. When creating logs we saw the term ‘logger’ when we discussed the name of the log, as well as creating a log using the FileHandler() function. Let’s dive deeper into what it means as well as a comprehensive look at the classes and functions we use and can use when creating logs.

The logger is a term that represents an object of the ‘Logger’ class, and we use this class to create logs. In all of our previous blogs regarding logging, we used an instance of the ‘Logger’ class with its default values. Like any class, we too can create an object of the class with custom values.

‘LogRecord’ is a class that holds the information related to the log, such as its name, severity level, path, line number, the function from which it was called, and a few more. An instance of this class is created automatically whenever a new log is created.

Another class to keep in mind is the ‘Handler’ class which sends the log records created by loggers to the required destination. Specifically, we use this addHandler() function to add handler objects. Also, ‘Handler’ serves as the base class with which we can use subclasses such as StreamHandler, FileHandler, HTTPHandler, etc.

 

Logger Functions

We have already seen 5 functions from this class namely debug(), info(), warning(), error(), and critical(). Let’s consider a couple more functions from the logger class.

FUNCTION 1: setlevel()

This function is used to set the minimum severity level of the log.

import logging

# Logger Object
logger = logging.getLogger()

# set logging level as INFO
logger.setLevel(logging.WARNING)

logger.debug("Debug log")
logger.info("Info log")
logger.warning("Warning raised")
logger.error("Error occurred here")
logger.critical("Critical log message"

FUNCTION 2: log(level, msg)

This function allows us to log a message using the severity level we provide with the level parameter.

import logging

# Logger Object
logger = logging.getLogger()

logger.log(level=30, msg="Logging on Warning using log fn.")

FUNCTION 3: hasHandlers()

This checks to see if any handlers have been configured for a particular logger.

# Logger Object
logger = logging.getLogger()

print(logger.hasHandlers())  # Prints False

Let’s try this again but this time, we will add the code of our FileHandler() function once again and see the output.

import logging

# Logger Object
logger = logging.getLogger()

# Created a file handler
handler = logging.FileHandler('logFile.log')

# Adding a handler to the logger
logger.addHandler(handler)

logger.error("Error occurred")

print(logger.hasHandlers())  # Prints True

FUNCTION 4: removeHandler(hdlr)

Removes the specified handler from our logger.

import logging

# Logger Object
logger = logging.getLogger()

# Created a file handler
handler = logging.FileHandler('logFile.log')

# Adding a handler to the logger
logger.addHandler(handler)

# Removing handler
logger.removeHandler(handler)

logger.error("Error occurred")

print(logger.hasHandlers())  # Now prints False

This gives us:

This time instead of saving the log record onto our file, we now have the log printed onto the console and we can also see that the logger object has no more handlers.

FUNCTION 5: isEnabledFor(level)

Checks if log messages for the specified severity level would be processed or not.

# Logger Object
logger = logging.getLogger()

# Minimum severity level
logger.setLevel(logging.WARNING)

print(logger.isEnabledFor(logging.INFO))  # Prints False
print(logger.isEnabledFor(logging.WARNING))  # Prints True

What have we learned?

  • What does the term logger represent?
  • What is the LogRecord class?
  • What is the Handler class?
  • What does the setlevel() function achieve?
  • How does the isEnabledFor() function work?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments