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

What is the log module in Python?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what the log module in Python is, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

The role of the journal

When it comes to diaries, we can imagine them as diaries in real life. Diary is a way for us to record every bit of our life, and log we can think of as the diary of the program, the diary of the program is used to record the behavior of the program, generally speaking, we can record some important information of the program through the log.

For example, where was the wrong report? What is the reason for reporting an error? At this point, we can find out what went wrong and what caused it by looking at the log, which can help us quickly detect errors and fix bug.

In fact, it is not just the wrong message, we can also log the status of the program running.

The level of the log

Now that the role of the log is clear, we can record different levels of logs according to our different business needs in the usual logging process.

Debug: it can help us check whether some of the output information is correct during the normal development process. It can replace the print () function that we usually use.

Info: it represents general message class information, just to record the behavior of some programs, such as program execution to a certain location, to make some simple records.

Warnning: this level is a warning, generally speaking, the program will not make mistakes, but there may be some potential risks.

Error: there are major problems in the general corresponding business. For example, exceptions or business logic should not be executed to a certain situation. We can all record it through error.

Critical: more severe than error, but generally speaking the error level is already very serious, so critical is rarely used.

The use of logging module

In fact, the logging module is quite complex to use, but we just need to learn to use logging.basicConfig to meet our most basic logging functions.

Next, let's take a look at what parameters logging.basicConfig needs to pass in and how to use them.

The lowest level lever=logging.DEBUGformat log output format of lever log output is shown in the following format specific format filename storage location filename='d://debug.log'filemode input mode filemode= "w"

With regard to lever, the role of lerver is to represent the lowest logging level record. For example, lever=logging.DEBUG above means that all log levels, including the DEBUG level, are recorded.

The specific format of format is as follows:

Format character meaning% (levername) s log level name% (pathname) s path to the currently executing program (that is, location of the script)% (filename) s execute script program name% (lineno) d log current line number% (asctime) s time to print log% (message) s log information

Common log fromat scheme: fromat ='% (asctime) s% (filename) s [line:% (lineno) d]% (levername) s% (message) s'

The logging module demonstrates a small case import logginglogging.basicConfig (# configure for basicConfig (basicConfig is actually a dynamic adjustment to the logging module After that, you can directly use) level=logging.INFO, logs below # INFO level will not be recorded format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) log, # log output format filename='back.log', # log storage path (stored in the current relative path) filemode='w', # input mode If our file already exists, we can use the'a 'mode instead of the' w' mode # similar to the mode in which the file is written, which creates the file when there is no file Logging.debug ('this is a debug message') logging.info ('this is a log message') logging.warning ('this is a warning message') logging.error ('this is a major error message') # > the execution result is as follows

The filemode mode we used above is'w' mode, but now we change to'a' mode and try to append the log to the back.log file.

Import logginglogging.basicConfig (# configure for basicConfig (basicConfig is actually a dynamic adjustment to the logging module After that, you can directly use) level=logging.INFO, logs below # INFO level will not be recorded format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) log, # log output format filename='back.log', # log storage path (stored in the current relative path) filemode='a', # input mode If our file already exists, we can use the'a 'mode instead of the' w' mode # similar to the mode in which the file is written, which creates the file when there is no file Logging.debug ('this is a debug message-- >\' a\ 'mode second write') logging.info ('this is a log record information-- >\' a\ 'mode second write') logging.warning ('this is a warning message-- >' a\ 'mode second write') ') logging.error (' this is a major error message->\'a\ 'mode second write') # > the execution result is as follows

Here, as to whether the file exists and whether to use the'w' mode or'a 'mode, we can judge the path file through the os module, so let's optimize the script above.

Import loggingimport osdef init_log (path): if os.path.exists (path): mode ='a 'else: mode =' w' logging.basicConfig (# configure for basicConfig (basicConfig is actually a dynamic adjustment to the logging module After that, you can directly use) level=logging.INFO, logs below # INFO level will not be recorded format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) log, # log output format filename='back.log', # log storage path (stored in the current relative path) filemode=mode, # input mode If our file already exists, we can use the'a 'mode instead of the' w' mode # similar to the mode in which the file is written, which creates the file when there is no file Return loggingcurrent_path = os.getcwd () path = os.path.join (current_path, 'back.log') log = init_log (path) # initialize the returned init_log () function Actually, it is return logginglog.debug ('this is a debug message-- > the third write') log.info ('this is a log record-- > the third write') log.warning ('this is a warning message-- > the third write') log.error ('this is a major error message-- > the third write') # > the execution result is as follows:

Thank you for reading this article carefully. I hope the article "what is the log module in Python" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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