Use python's logging module and pythonlogging Module
I. Starting from a use case
To develop a log system, you must output logs to the console and write log files.
Python code
- Import logging
- # Create a logger
- Logger = logging. getLogger ('mylogger ')
- Logger. setLevel (logging. DEBUG)
- # Create a handler for writing log files
- Fh = logging. FileHandler ('test. log ')
- Fh. setLevel (logging. DEBUG)
- # Create another handler for output to the console
- Ch = logging. StreamHandler ()
- Ch. setLevel (logging. DEBUG)
- # Define the handler output format
- Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
- Fh. setFormatter (formatter)
- Ch. setFormatter (formatter)
- # Add handler to logger
- Logger. addHandler (fh)
- Logger. addHandler (ch)
- # Record a log
- Logger.info ('foorbar ')
After running, there is a log in the console and the log file:
Java code
- 19:18:29, 816-mylogger-INFO-foorbar
Ii. APIs of the logging Module
Based on the example above, let's talk about several of the most commonly used APIs.
Logging.GetLogger([Name])Returns a logger instance. If no name is specified, the root logger is returned. As long as the names are the same, the returned logger instances are the same and only one, that is, the names and logger instances correspond one to one. This means that you do not need to pass the logger instance in each module. You only need to know the name to get the same logger instance.
Logger.SetLevel(Lvl)Set the logger level. The level has the following levels:
NOTSET <DEBUG <INFO <WARNING <ERROR <CRITICAL
If the looger level is set to INFO, no logs smaller than INFO level are output, and logs larger than or equal to INFO level are output.
Python code
- Logger. debug ("foobar") # No output
- Logger.info ("foobar") # output
- Logger. warning ("foobar") # output
- Logger. error ("foobar") # output
- Logger. critical ("foobar") # output
Logger.AddHandler(Hdlr)Logger can employ handler to process logs. handler mainly has the following types:StreamHandler: output to the consoleFileHandler: output to fileHandler can also set its own level and output format.
Logging.BasicConfig([** Kwargs])* This function is used to configure the root logger, create a StreamHandler for the root logger, and set the default format. * These functions: logging. debug (), logging.info (), and logging. warning (), logging. error (), logging. critical () if the root logger does not have any handler when it is called, it will automatically call basicConfig to add a handler * If the root logger already has a handler, this function will not do anything
Use basicConfig to configure the output format and level of the root logger:
Python code
- Import logging
- Logging. basicConfig (format = '% (levelname) s: % (message) s', level = logging. DEBUG)
- Logging. debug ('this message shoshould appear on the console ')
Iii. relationship between root logger and logger
As mentioned previously, the root logger instance has a parent-child relationship. The root logger is the top-level logger, which is the ancestor of all logger. For example:Root logger is the default logger.If you do not create a logger instance, you can directly call logging. debug (), logging.info () logging. warning (), logging. error (), logging. critical () functions, the logger used is the root logger, which can be automatically created and is also a single instance.
How to obtain the root loggerUse logging. getLogger () or logging. getLogger ("") to obtain the root logger instance.
Default levelThe default level of root logger is logging. WARNING.
How to represent a parent-child relationshipThe name of a logger can indicate the parent-child relationship between logger. For example, parent_logger = logging. getLogger ('foo') child_logger = logging. getLogger ('foo. bar ')
What is dynamic level?Logger has a concept called valid level. If a logger does not explicitly set the level, it uses the level of the father. If the father does not explicitly set the level, he will use his father's level to push... and finally reach the root logger. The level must have been set. The default value is logging. WARNINGchild. After obtaining a message, the loggers distribute the message to the handler and send it to all the previous logger for processing,
Let's look at an example.
Python code
- Import logging
- # Set root logger
- R = logging. getLogger ()
- Ch = logging. StreamHandler ()
- Ch. setLevel (logging. DEBUG)
- Formatter = logging. Formatter ('% (asctime) s-% (levelname) s-% (message) s ')
- Ch. setFormatter (formatter)
- R. addHandler (ch)
- # Create a logger as the father
- P = logging. getLogger ('foo ')
- P. setLevel (logging. DEBUG)
- Ch = logging. StreamHandler ()
- Ch. setLevel (logging. DEBUG)
- Formatter = logging. Formatter ('% (asctime) s-% (message) s ')
- Ch. setFormatter (formatter)
- P. addHandler (ch)
- # Create a child logger
- C = logging. getLogger ('foo. bar ')
- C. debug ('foo ')
The output is as follows:
Python code
- 21:04:29, 893-foo
- 21:04:29, 893-DEBUG-foo
It can be seen that the child logger does not have any handler, so the message is not processed. However, it forwards the message to its father and root logger. Finally, two logs are output.
Where is the default log file stored in the logging module of python?
When configuring log file objects, you need to fill in the location of the log file. I put them all according to project requirements.
No research is performed by default.
Does python logging not output to the screen?
So where do you want to output it? You can intercept sys. stdout.