How to log using Python's logging module
Logging is a very important part of writing Python programs. Logs not only help you debug code during development, but also provide diagnostic information when the program is officially run to help locate problems. If you are building a complex system or developing large applications, logging is an indispensable tool. Python provides a powerful and flexible logging module, through which you can record various types of log information, from debug information to error reports.
In this article, we will introduce Python's logging module to help you understand how to use this module to record and manage logs. We will start from the basics and gradually expand to more advanced usage methods, suitable for beginners to read.
Why use logs?
Before we start, let's first look at why we use logs. Debugging is often inevitable when writing programs. Beginners may tend to useprint()
Statements to output debugging information, which can indeed play a role in a simple program. But as the complexity of the program increases,print()
The disadvantages gradually emerge:
-
Uncontrollable:When you have a lot
print()
When statements, they appear everywhere in the terminal and are difficult to manage. - Poor flexibility: You cannot easily adjust what information should be output, saved to a file, or sent to other places.
-
Unable to distinguish log levels:
print()
The importance of output cannot be distinguished. For example, debug information and error information are mixed together.
By comparison,logging
Modules have the following advantages:
-
Multiple log levels:
logging
A variety of log levels are provided, such as DEBUG, INFO, WARNING, ERROR and CRITICAL, to help you distinguish logs based on importance. - Flexible output method: Logs can be output to the console, files, and even sent to remote servers over the network.
- Strong configurability: You can customize the behavior of logs by configuring different loggers, processors, and formatters.
After understanding these backgrounds, let’s take a step-by-step understanding of how to use them.logging
Module.
Basic use
First, let's start with how to log a simple log. To uselogging
The module, first you need to import it and set the basic configuration of logging.
import logging
logging
The module provides a simplebasicConfig()
Function, used to set basic log configuration. You can specify the output format, log level, and output location (such as a file or console).
Record a simple log
We can start with the most basic example and record a simple log:
import logging (level=) ("This is a debugging information") ("This is a message") ("This is a warning") ("This is a mistake") ("This is a serious mistake")
In this example,basicConfig()
Set the log level toDEBUG
, which means fromDEBUG
Information at levels and higher important levels will be recorded and output to the console. Each log level indicates a different severity:
-
DEBUG
: Debugging information, the most detailed log, used to diagnose problems. -
INFO
: Normal runtime messages, such as information about the start or end of the program. -
WARNING
: Indicates a potential problem and the program may still be running normally. -
ERROR
: Error message, but the program can still continue to run. -
CRITICAL
: A serious error, indicating that the program may not be able to continue running.
The above code output is as follows:
DEBUG:root:This is a debugging information INFO:root:This is a message WARNING:root:This is a warning ERROR:root:This is an error CRITICAL:root:This is a serious mistake
Output to file
In some cases, we want to output the logs to a file, not just displaying them in the console. We can passbasicConfig()
offilename
Parameters to achieve this:
(filename='', level=)
Now all log information will be written toFile, not displayed in the console. To avoid the log file becoming too large, you can also set a size limit for the log file or perform log rotation by time (we will introduce it later).
Configure log format
The default log format may not be intuitive or easy to read. Fortunately,logging
The module allows us to passformat
Parameters customize the output format of the log. A common log format may include timestamps, log levels, messages, etc.
( level=, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S' )
In this example, the format of the log is set to:
Timestamp - Logger Name - Log Level - Message
The output may look like this:
2024-10-07 10:45:23 - root - INFO - This is a message 2024-10-07 10:45:23 - root - WARNING - This is a warning
Log format description
-
%(asctime)s
: Output the current time. -
%(name)s
: The name of the logger, usuallyroot
, if not specially set. -
%(levelname)s
: Log level name, such asINFO
、WARNING
。 -
%(message)s
: The specific message content of the log.
By customizing these format placeholders, we can make the logs clearer and easier to read.
Log processor and formatter
logging
The power of the module is that it uses a flexible hierarchy to manage logs. These hierarchies consist of three main parts:
-
Logger: The entry used to generate log messages, usually through
()
Create. - Processor (Handler): Responsible for sending log messages to the appropriate output location (such as console, file, or network).
- Formatter: Define the output format of the log.
Using different processors
In addition to usingbasicConfig()
Simplely set up the logs, we can also manually configure the logger and processor to achieve more detailed control. Here is an example of using a console and a file processor:
logger = ('my_logger') () # Create a console processorconsole_handler = () console_handler.setLevel() # Create a file processorfile_handler = ('') file_handler.setLevel() # Create a formatter and add it to the processorformatter = ('%(asctime)s - %(name)s - %(levelname)s - %(message)s') console_handler.setFormatter(formatter) file_handler.setFormatter(formatter) # Add processor to logger(console_handler) (file_handler) # Test log output('This is a debugging information') ('This is a message') ('This is a warning') ('This is a mistake') ('This is a serious mistake')
In this example, we create a custom logger and set up two processors: one for outputting the logs to the console and the other for outputting the logs to the file. Different processors can set different log levels. For example, the console processor only recordsWARNING
Logs of level and above, while the file processor records all logs.
Log rotation
Log files can become very large in long running programs. To prevent the log file from being too large, you can use the log rotation mechanism. Python providesRotatingFileHandler
, you can automatically create a new file when the file reaches the specified size:
from import RotatingFileHandler rotating_handler = RotatingFileHandler('', maxBytes=2000, backupCount=5) (rotating_handler)
In this example,The maximum file is 2000 bytes. When the log file exceeds this size, a new file will be automatically created, and up to 5 old files will be retained.
Conclusion
Python's logging module is a powerful and flexible tool for projects of all sizes. From simple console logs to complex multiprocessor and multi-formater logging systems, it can be easily competent. For beginners, understanding the basic concepts and usage of logging is an important programming skill.
In actual projects, logs can not only help you debug the code, but also record important information when the program runs, helping you quickly locate and resolve problems when there is a problem with the program. Therefore, mastering the skills of logging is of great benefit to improving programming efficiency.
The above is the detailed content of Python's operation of using the logging module to record logs. For more information about python logging logging, please pay attention to my other related articles!