Logging is a method of recording events that occur when software is run. Logging is essential for the development, debugging, and operation of software. If you don’t have a logging record and your programme crashes, there’s a slim chance you’ll find out what went wrong. And determining the cause will take a significant amount of time. Logging allows you to leave a breadcrumb trail so that if something goes wrong, we can pinpoint the source of the problem.
There are a variety of scenarios, such as expecting an integer but being given a float, using a cloud API while the service is down for maintenance, and so on. Such issues are out of hand and difficult to manage.
The Python logging module is a ready-to-use and powerful module that is intended to meet the needs of both beginners and enterprise teams. It is used by the majority of third-party Python libraries, so you can combine your log messages with those from those libraries to create a unified log for your application.
To use this module you just need to write the following:
import logging
The five built-in levels of log messages in the Python logging
module are:
DEBUG
: Detailed information, typically of interest only when diagnosing problems.INFO
: Confirmation that things are working as expected.WARNING
: Indication that something unexpected happened, or is indicative of some problem in the near future.ERROR
: Due to a more serious problem, the software has not been able to perform some functions.CRITICAL
: Serious error, indicating that the program itself may be unable to continue running.When configuring the logging system, you can specify the level of messages that should be logged by setting the level
parameter of the basicConfig()
function. For example, if you set the level to logging.WARNING
, only messages with a level of WARNING
, ERROR
, and CRITICAL
will be logged.
It’s also possible to create custom log levels, by using constants from logging
module. These levels are in between the predefined levels and can be used to provide more detailed information about the log messages.
The Python logging
module provides several methods for logging messages. The most commonly used methods are:
debug()
: Logs a message with level DEBUG
.info()
: Logs a message with level INFO
.warning()
: Logs a message with level WARNING
.error()
: Logs a message with level ERROR
.critical()
: Logs a message with level CRITICAL
.Here’s an example of how to use these methods:
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
Output:
023-01-22 18:20:29,386 - root - DEBUG - This is a debug message
2023-01-22 18:20:29,386 - root - INFO - This is an info message
2023-01-22 18:20:29,386 - root - WARNING - This is a warning message
2023-01-22 18:20:29,386 - root - ERROR - This is an error message
2023-01-22 18:20:29,386 - root - CRITICAL - This is a critical message
Additionally, logging
module also provides a method log()
which is a general-purpose logging method that can log messages of any severity level.
logging.log(logging.DEBUG, 'This is a debug message')
It’s also possible to use the basicConfig
method to configure logging to different handlers, that can output logs to different destinations such as file, remote server, etc.
It’s also possible to create custom logging methods by creating logger instances and specifying the level, handlers and formatters for it.
Note: also read about Python Shallow and Deep Copy
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…