Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the python logging log module

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "python logging log module how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python logging log module how to use" it!

1. Brief introduction to Log

When it comes to logging, whether it is writing framework code or business code, it is inseparable from the log record, and it can bring great help to us to locate the problem.

The easiest way to keep a log is to add a print where you want to record it. I'm sure both beginners and veterans do this all the time. There is no problem doing this in simple code or in small projects. But in some larger projects, sometimes to locate a problem, you need to check the history log to locate the problem, so it is inappropriate to use print.

The log printed by print has no time, does not know the location of the log record, does not have a readable log format, and cannot output the log to the specified file. Unless you repeat all these by yourself.

The best practice is to use the built-in logging module, because the logging module provides developers with a wealth of functionality.

For example, the above picture records the generated log with the standard library logging module, including the specific time of the log, the module in which the log occurs, the log level and the specific content of the log, and so on.

How to use it, let's look at an example:

Import the logging module and then directly use the logging message logging method provided by logging.

two。 Log level

The log level is divided into the following five levels

The log level uses the scenario DEBUGdebug level to record detailed information to facilitate debugging. In the production environment, we generally do not turn on DEBUGINFO to record the information of key code points, so that the code is executed as expected. The production environment usually sets the INFO level WARNING to record some unexpected situations. Such as insufficient disk ERROR information recorded when some functions cannot run properly due to a more serious problem CRITICAL when a serious error occurs, resulting in the application cannot continue to run

The importance of log levels increases one by one, and python provides five corresponding levels of methods. By default, the log level is WARGING, and log information lower than WARING will not be output.

You can see from the above code that the log contents after loging.warging are printed in the standard output stream, that is, the command line window, but the logs recorded by logging.debug and info are not printed.

3. Modify log level

How do I output debug-level information?

Of course, modify the default log level. You can use the logging.basicConfig method to set the log level before you start logging.

Import logginglogging.basicConfig (level=logging.DEBUG) logging.debug ("this is debug") logging.info ("this is info") logging.error ("this is error")

When set to the debug level, all log information is output

DEBUG:root:this is debugINFO:root:this is infoERROR:root:this is error4. Log to file

The previous log will output the log to the standard output stream by default, only in the command line window, and there is no place to find the history log after the program is restarted, so it is a common requirement to record the log content permanently. You can also specify where the log is output through the configuration function logging.basicConfig.

Import logginglogging.basicConfig (filename= "test.log", level=logging.INFO) logging.debug ("this is debug") logging.info ("this is info") logging.error ("this is error")

Here, I specify that the log is output to the file test.log, and the log level is specified as INFO. The content recorded in the final file is as follows:

INFO:root:this is infoERROR:root:this is error

Each re-run, the log will be appended. If you want to overwrite the previous log before each run, you need to specify that the parameter filemode='w', is the same as that used by the open function to write data to the file.

5. Specify log format

The default output format consists of three parts, the log level, the name of the logger, and the log contents, with ":" in the middle. If we want to change the log format, such as adding the date and time and displaying the logger name, we can specify the format parameter to set the log format.

Import logginglogging.basicConfig (format='% (asctime) s% (levelname) s% (name) s% (message) s') logging.error ("this is error")

Output:

2021-12-1507 ERROR root this is error 44 purl 16547

Log format output provides a lot of parameters, in addition to the time, log level, log message content, logger name, you can also specify thread name, process name, and so on.

So far, this is the basic use of the log module, and it can also meet most application scenarios. More advanced methods can help you deal with logs better.

6. Recorder (logger)

The logging described above is actually created through an instance object called Logger. Each logger has a name. When logging is directly used by logging, the system will create a logger named root by default, which is the root logger. The logger supports hierarchical structure, and the child logger usually does not need to set the log level and Handler separately (described later), and if the child logger is not set separately, its behavior is delegated to the parent.

The logger name can be any name, but the best practice is to use the module name directly as the logger name.

It is named as follows:

Logger = logging.getLogger (_ _ name__)

By default, the logger uses a hierarchical structure, with the previous period arranged as a delimiter in the hierarchy of the namespace. The logger at the bottom of the hierarchical list is a child of the logger higher in the list. For example, there is a logger called foo, and the name is foo.bar,foo.bar.baz, and the loggers of foo.bam are both children of foo.

├─ foo

│ │ main.py

│ │ _ _ init__.py

│ │

│ ├─ bam

│ _ _ init__.py

│ │ │

│ │

│ ├─ bar

│ _ _ init__.py

│ │ │

│ │ ├─ baz

│ _ _ init__.py

│ │

Main.py:

Import foofrom foo import barfrom foo import bamfrom foo.bar import bazif _ _ name__ = ='_ _ main__': pass

Foo.py

Import logginglogging.basicConfig () logger = logging.getLogger (_ name__) logger.setLevel (logging.INFO) logger.info ("this is foo")

Here I only set the level of the foo recorder to INFO

Bar.py

Import logginglogger = logging.getLogger (_ _ name__) logger.info ("this is bar")

The other sub-modules are similar code like bar.py, with no log level set, and the final output is

INFO:foo:this is fooINFO:foo.bar:this is barINFO:foo.bam:this is bamINFO:foo.bar.baz:this is baz

This is because the foo.bar logger does not set the log level, so it will find the ancestors who have set the log level. Here, you can find the parent logger foo whose level is INFO. If foo is not set, you will find the root logger root,root whose default level is WARGING.

7. Processor (Handler)

The logger is responsible for recording the log, but it doesn't care where the log is finally recorded, but leaves it to another guy, the Handler.

For example, in a Flask project, you might log INFO-level logs to a file, ERROR-level logs to standard output, and send some critical logs (such as orders or serious errors) to an email address to notify the boss. At this point your logger adds several different processors to handle different message logs to send specific locations according to the importance of the message.

Python has many practical processors built into it, some of which are commonly used

1. StreamHandler standard stream processor, sending messages to standard output stream and error stream

2. FileHandler file processor, sending messages to files

3. RotatingFileHandler file processor, after the file reaches the specified size, enable the new file to store the log

4. TimedRotatingFileHandler file processor, log rotates log files at specific intervals

8. Processor operation

Handler provides four methods for developers to use. As you can see carefully, logger can set level,Handler or Level. Through setLevel, different levels of messages recorded by the logger can be sent to different places.

Import loggingfrom logging import StreamHandlerfrom logging import FileHandlerlogger = logging.getLogger (_ _ name__) # set to DEBUG level logger.setLevel (logging.DEBUG) # standard stream processor, set to WARAINGstream_handler = StreamHandler () stream_handler.setLevel (logging.WARNING) logger.addHandler (stream_handler) # file processor Set the level to INFOfile_handler = FileHandler (filename= "test.log") file_handler.setLevel (logging.INFO) logger.addHandler (file_handler) logger.debug ("this is debug") logger.info ("this is info") logger.error ("this is error") logger.warning ("this is warning")

After running, the log output in the command line window is as follows:

This is errorthis is warning

The log output in the file is as follows:

This is infothis is errorthis is warning

Although we set the level of logger to DEBUG, the message logged by debug was not output because I set a higher level for both Handler than DEBUG, so the message was filtered out.

9. Formatter (formatter)

The formatter was actually introduced earlier in the article, but it is specified through logging.basicConfig. In fact, the formatter can also be set on the Handler as an object. The formatter can specify the output format of the log, whether to display the time, what the time format, whether to display the level of the log, whether to display the name of the recorder, and so on.

Import loggingfrom logging import StreamHandlerlogger = logging.getLogger (_ _ name__) # standard stream processor stream_handler = StreamHandler () stream_handler.setLevel (logging.WARNING) # create a formatter formatter = logging.Formatter ('% (asctime) s -% (name) s -% (levelname) s -% (message) s') # stream_handler.setFormatter (formatter) # add processor logger.addHandler (stream_handler) logger.info (" This is info ") logger.error (" this is error ") logger.warning (" this is warning ")

Note: the formatter can only be used on the processor and the formatter can be set through the processor's setFromatter method. And only one formatter can be set for a Handler. It's an one-on-one relationship. Logger and handler have an one-to-many relationship, and multiple handler can be added to a logger. Both handler and logger can set the log level.

10.logging.basicConfig

Going back to the beginning, what did the logging.basicConfig () method do for us? Now you can probably guess. Let's see what the python source code says.

Do basic configuration for the logging system.

This function does nothing if the root logger already has handlers configured. It is a convenience method intended for use by simple scripts to do one-shot configuration of the logging package.

The default behaviour is to create a StreamHandler which writes to sys.stderr, set a formatter using the BASIC_FORMAT format string, and add the handler to the root logger.

A number of optional keyword arguments may be specified, which can alter the default behaviour.

1. Create a root recorder

2. Set the log level of root to warning

3. Add a StreamHandler processor to the root recorder

4. Set a simple formatter for the processor

Logging.basicConfig () logging.warning ("hello")

These two lines of code are actually equivalent to:

Import sysimport loggingfrom logging import StreamHandlerfrom logging import Formatterlogger = logging.getLogger ("root") logger.setLevel (logging.WARNING) handler = StreamHandler (sys.stderr) logger.addHandler (handler) formatter = Formatter ("% (levelname) s formatter% (name) s") handler.setFormatter (formatter) logger.warning ("hello")

What the logging.basicConfig method does is equivalent to a basic configuration for the log system, making it easy for developers to quickly access and use it. It must be called before logging starts. However, if the root logger has specified another processor and you call basciConfig at this time, the method will fail and it will do nothing.

11. Log configuration

In addition to the configuration described earlier, the configuration can be written directly in the code, and the configuration information can be placed separately in the configuration file to achieve the separation of the configuration and the code.

Log profile logging.conf

[loggers] keys= rootHandler keys= [formatters] keys= simpleFormatter [logger _ root] level=DEBUGhandlers= consoleHandler [handler _ consoleHandler] class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs= (sys.stdout,) [formatter_simpleFormatter] format=% (asctime) s -% (name) s -% (levelname) s -% (message) s

Load configuration file

Import loggingimport logging.config# load configuration logging.config.fileConfig ('logging.conf') # create loggerlogger = logging.getLogger () # Application Code logger.debug ("debug message") logger.info ("info message") logger.warning ("warning message") logger.error ("error message")

Output:

2021-12-23 00 debug message 02lo 07019-root-DEBUG-debug message

2021-12-23 00 info message 02lo 07019-root-INFO-info message

2021-12-23 00 warning message 02lo 07019-root-WARNING-warning message

2021-12-23 00 error message 02lo 07019-root-ERROR-error message

Thank you for your reading, the above is the content of "how to use the python logging log module". After the study of this article, I believe you have a deeper understanding of how to use the python logging log module, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report