Categories: python

Python Logging Classes and Functions

In Python, the logging module provides several classes and functions to help you log messages in your code. We can create our own logger by instantiating the logging.Logger class and giving it a unique name. This is useful when your application has multiple modules, as you can use separate loggers for each module to keep the log messages separate and organized. Here are a few of the most commonly used ones:

  • logging.basicConfig(**kwargs): This function sets up the basic configuration of the logging system, such as the logging level, the output destination, and the format of the log messages.
  • logging.debug(msg, *args, **kwargs): This function logs a message at the DEBUG level. The message can be a string, and any additional arguments will be passed through to the underlying logging method.
  • logging.info(msg, *args, **kwargs): This function logs a message at the INFO level. The message can be a string, and any additional arguments will be passed through to the underlying logging method.
  • logging.warning(msg, *args, **kwargs): This function logs a message at the WARNING level. The message can be a string, and any additional arguments will be passed through to the underlying logging method.
  • logging.error(msg, *args, **kwargs): This function logs a message at the ERROR level. The message can be a string, and any additional arguments will be passed through to the underlying logging method.
  • logging.critical(msg, *args, **kwargs): This function logs a message at the CRITICAL level. The message can be a string, and any additional arguments will be passed through to the underlying logging method.
  • logging.Logger: This class represents a logger and is used to log messages in your code. You can use the basicConfig() function to set up the logger and then use its methods (such as debug(), info(), etc.) to log messages.

Here are a few examples of commonly used classes in the logging module:

  • logging.StreamHandler: This class sends log messages to a stream, such as sys.stdout or sys.stderr.
  • logging.FileHandler: This class is used to send log messages to a file.
  • logging.handlers.RotatingFileHandler: This class is used to send log messages to a file and rotate the file when it reaches a certain size.
  • logging.handlers.SMTPHandler: This class is used to send log messages via email using the Simple Mail Transfer Protocol (SMTP).
  • logging.Formatter: This class is used to format log messages before they are sent to the output destination.

These classes can be used to configure the logger, and to customize the log messages, format, and output destinations.

Example:

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('example.log')
file_handler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.debug("This is a debug message")
logger.error("This is an error message")

In the above example, a logger is created with the name of the module. Then the level of the logger is set to debug and a file handler is added with the level of error. The Formatter class is used to format the log message and set to the file handler.

In addition to these classes and functions, the logging module also provides several other features, such as the ability to configure loggers using configuration files, and the ability to add custom handlers to control where log messages are sent.

Note: also read about Python Logging Variables Data

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago