See how you stack up against top hiring criteria for the role in 2025.
Compare against 1000+ live job postings
Identify critical technical skill gaps
Get a personalized improvement roadmap
No signup required, takes less than 30 sec
Introduction
Logging in Python means tracking events that happen when some software runs. It is very important for software development, debugging, and running.
In this article, we will learn about Python logging and how logging works in Python using various Python logger objects. We will also cover the levels of logs. Let’s first understand what logging is.
What is logging?
The process of keeping a record of all input data, output data, processes, and final results of a program is known as Logging. It helps the developer track any issues or events when a specific program is executed. It is necessary for the creation, operation, and debugging of software.
Logging in Python is easy and fast. The ability to interact with the framework for releasing log messages from Python programmes is provided by Python logging. Python logging module defines all the functions and classes and develops a scalable event-logging system for applications.
Here is an example of logging in Python:
Example
>>> import logging
>>> logging.warning('This is a warning message!')
WARNING:root:This is a warning message!
Python Logging Basic Terminology
Before moving forward, let’s discuss some basic terminology used in Python logging.
Loggers: These main components offer various ways to enable runtime logging from your apps.
LogRecord: The logger name, related function, the log message, the line number, and other details are all included in the LogRecord objects that loggers automatically create.
Formatters: It is useful to add extra content to the log message.
Handlers: It is used to display the log message effectively.
Log messages vary widely from one another. Python documentation explains six logging levels. Each level is associated with an integer value. Let’s discuss them one by one.
CRITICAL
Indicates a major error that may prevent the programme from continuing to operate. Integer value: 50
ERROR
The word "ERROR" is used to indicate when there is a major problem. Integer value: 40
WARNING
It is used to indicate that something unexpected has occurred. Integer value: 30
INFO
It gives us information about whether or not our desired outcomes are being achieved. Integer value: 20
DEBUG
It is only used when troubleshooting issues. Integer value: 10
NOTSET
It is the default level when a new logger is created. Integer value: 0
Here is an example of different Python logging levels:
Explanation: The format of the Logging level is level, name, and log message separated by colon(:). Before each message in the output, root—the name the logging module gives to its default logger—is also displayed.
Note: There was no logging of the info() and debug() messages. This is so that the logging module can record messages with a severity level of WARNING or higher by default.
Some of the important logger objects are as follows:
Propagate: Events reported to this logger will be forwarded to any handlers linked to it as well as any handlers of higher-level loggers if this property evaluates to true.
setLevel(level): It sets the logger's threshold to level.
getEffectiveLevel(level): Specifies the logger's effective level. If setLevel() was used to set a value other than NOTSET, that value is returned.
isEnablefor(level): It shows whether the logger will process a message based on its severity level.
log(level, msg, *args, **kwargs): It sends a message to the logger with the integer level.
critical(msg, *args, **kwargs): It sends a message to the logger that is marked as CRITICAL.
error(msg, *args, **kwargs): It sends a message to the logger that is marked as ERROR.
warning(msg, *args, **kwargs): It sends a message to the logger that is marked as a WARNING.
info(msg, *args, **kwargs): It sends a message to the logger that is marked as INFO.
debug(msg, *args, **kwargs): It sends a message to the logger that is marked as DEBUG.
addHandler(handler): It adds the desired handler to the logger.
removeHandler(handler): It removes the desired handler from the logger.
hasHandlers(): It determines whether this logger has any configured handlers.
handle(record): It passes a record to all handlers connected to this logger and its ancestors to handle it. Unpickled records created locally, as well as those received from a socket, are both processed using this method.
addFilter(filter): It adds the desired filter to the logger.
removefilter(filter): It removes the desired filter from the logger.
filter(record): If the record needs to be processed, apply the filters of this logger to it and return True.
Example of Python Logging
Here is an example of Python Logging.
Python
Python
import logging # Creating an object logger = logging.getLogger()
# Setting the threshold of the logger to DEBUG logger.setLevel(logging.DEBUG)
Explanation: Here, we also get the log message for Info and debug levels because we are changing the configuration using the setLevel object.
Advantage of Logging over Printing
To check whether the statements were performed correctly or if an error had occurred, some developers employ the idea of printing the statements. However, the idea of Python logging is much better than Printing statements.
The following are the advantages of logging over printing in Python:
Severity Level: Thanks to multiple logging levels, you can customise the severity level at which log messages are shown. That flexibility is not available to you with a print statement.
Separate files: Logging helps you point the log messages to separate files that can be used for post-analysis. On the other hand, it is not possible with printing.
Monitoring: Once the code is deployed, keep getting logs for better monitoring.
Frequently Asked Questions
What is Python Logging?
The ability to interact with the framework for releasing log messages from Python programs is provided by Python logging. Python logging module defines all the functions and classes and develops a scalable event-logging system for applications.
How to do proper logging in Python?
First, import the logging module. Then, create a log file. You should make a logger object for handling logging messages for the application. You can specify the logging format. This way, proper logging in Python is done.
Does Python Logging module use log4j?
No, the Python Logging module does not use log4f. It is not dependent on any other logging frameworks. But it was inspired by log4j, and there are similarities between their syntaxes. It is a standalone module.
What is the benefit of logging in Python?
The benefit of logging in Python is pinpointing the log messages to separate files that can be used for post-analysis. Also, after deployment, getting logs is beneficial for monitoring purposes. Also, the severity of log messages allows developers to control which messages to show.
Conclusion
In conclusion, The ability to interact with the framework for releasing log messages from Python programmes is provided by Python logging.
In this article, we extensively discussed Python Logging, logging levels, Logger objects and the advantage of logging.