Python's logging log module (i)

Source: Internet
Author: User
Tags integer division

Recently modified the logging related functions in the project and used the logging module in the Python standard library to make some records here. Mainly from official documents and StackOverflow to find some of the content.

    • Official documents

    • Technical Blog

Basic usage

The following code shows the most basic usage of logging.

#-*-Coding:utf-8-*-Import loggingImport Sys# Gets the logger instance and returns root Loggerlogger = Logging.getlogger If the argument is empty ("AppName")# Specify logger output Format formatter = logging. Formatter ('% (asctime) s% (levelname) -8s:% (message) s ')# file Log File_handler = logging. Filehandler ("Test.log") File_handler.setformatter (Formatter)# output formats can be specified by Setformatter# console Log Console_handler = logging. Streamhandler (sys.stdout) Console_handler.formatter = Formatter# You can also assign a value directly to formatter# Log Processor Logger.addhandler (File_handler) Logger.addhandler (Console_handler) added for logger# Specify the lowest output level of the log, default to warn level Logger.setlevel (logging.info)# output different levels of loglogger.debug (' This is debug Info ') Logger.info (' This is Information ') Logger.warn ("This is warning message") logger.error (' This is theerror message ') Logger.fatal (' This is fatal message, it's same as Logger.critical ') logger.critical (' This is critical message ') 
                         
                          # 2016-10-08 21:59:19,493 Info:this is information
                          # 2016-10-08 21:59:19,493 warning:this is WARNING message# 2016-10-08 21:59:19,493 error:this is ERROR message# 2016-10-08 21:59:19,493 critical:this was fatal message, it is Same as Logger.critical# 2016-10-08 21:59:19,493 critical:this is critical message# Remove some log processors Logger.removehand Ler (File_handler)     
                             

In addition to these basic uses, there are some common tips you can share.

Format output Log
# 格式化输出service_name = "Booking"logger.error(‘%s service is down!‘ % service_name)  # 使用python自带的字符串格式化,不推荐logger.error(‘%s service is down!‘, service_name) # 使用logger的格式化,推荐logger.error(‘%s service is %s!‘, service_name, ‘down‘) # 多参数格式化logger.error(‘{} service is {}‘.format(service_name, ‘down‘)) # 使用format函数,推荐# 2016-10-08 21:59:19,493 ERROR : Booking service is down!
Log exception information

When you use the logging module to record exception information, you do not need to pass in the exception object, as long as you directly call logger.error() or you logger.exception() can record the current exception.

# 记录异常信息try:    1 / 0except: # 等同于error级别,但是会额外记录当前抛出的异常堆栈信息 logger.exception(‘this is an exception message‘)# 2016-10-08 21:59:19,493 ERROR : this is an exception message# Traceback (most recent call last):# File "D:/Git/py_labs/demo/use_logging.py", line 45, in <module># 1 / 0# ZeroDivisionError: integer division or modulo by zero
Logging Configuration Essentials GetLogger () method

This is the most basic entrance, the method parameter can be empty, the default logger name is root, if in the same program has been using the same name logger, actually will get the same instance, using this technique can be used to call the same logger across modules to log logs.

Alternatively, you can use the log name to distinguish between different modules of the same program, such as this example.

logger = logging.getLogger("App.UI")logger = logging.getLogger("App.Service")
Formatter log format

The formatter object defines the structure and contents of the log information and requires two parameters to be constructed:

    • One is a formatted template fmt that contains the most basic level and information by default. message

    • One is the formatted time style datefmt , which defaults to2003-07-08 16:49:45,896 (%Y-%m-%d %H:%M:%S)

fmtYou can refer to the following table for the variables allowed in the

    • % (name) s Logger name

    • % (levelno) s log level in digital form

    • % (levelname) s log level in text form

    • % (pathname) s calls the full pathname of the module of the log output function and may not have

    • % (filename) s The file name of the module that called the log output function

    • % (module) s call the module name of the log output function |

    • % (funcName) s call the function name of the log output function |

    • % (Lineno) d the line of code where the statement of the log output function is called

    • % (created) F current time, represented by the UNIX standard floating-point number representing Time |

    • % (relativecreated) d When output log information, the number of milliseconds since logger was created |

    • % (asctime) s the current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds

    • % (thread) d thread ID. Probably not.

    • % (threadname) s thread name. Probably not.

    • % (process) d process ID. Probably not.

    • % (message) s user-output message

SetLevel Log Level

Logging has the following levels: Debug,info,warning,error,critical The default level is that the Warning,logging module will only output logs above the specified levels. The advantage of this is that in the development of the project debug log, in the product release phase without one by one comments, only need to adjust the level of logger can be, very convenient.

Handler Log Processor

The most commonly used are Streamhandler and Filehandler, handler used to log logs to different output ends. Logging contains many handler, which may be used in the following types of

    • Streamhandler Instances send error messages to Streams (File-like objects).

    • Filehandler Instances send error messages to disk files.

    • Rotatingfilehandler Instances send error messages to disk files, with support for maximum log file sizes and log File rotation.

    • Timedrotatingfilehandler Instances send error messages to disk files, rotating the log file at certain timed inte Rvals.

    • Sockethandler Instances send error messages to TCP/IP sockets.

    • Datagramhandler Instances send error messages to UDP sockets.

    • Smtphandler Instances send error messages to a designated email address.

Configuration method

There are several ways to configure logging.

    1. Complete configuration through the code, refer to the beginning of the example, mainly through the GetLogger method implementation.

    2. Simple configuration through the code, the following examples, mainly through the Basicconfig method implementation.

    3. Through the configuration file, here is an example, mainly throughlogging.config.fileConfig(filepath)

Logging.basicconfig

basicConfig()Provides a very convenient way for you to configure the logging module and start using it immediately, you can refer to the following example. Please refer to the official documentation for specific items that can be configured.

import logginglogging.basicConfig(filename=‘example.log‘,level=logging.DEBUG)logging.debug(‘This message should go to the log file‘)logging.basicConfig(format=‘%(levelname)s:%(message)s‘, level=logging.DEBUG)logging.debug(‘This message should appear on the console‘)logging.basicConfig(format=‘%(asctime)s %(message)s‘, datefmt=‘%m/%d/%Y %I:%M:%S %p‘)logging.warning(‘is when this event was logged.‘)

Note: In fact, you can not even configure anything directly using the default value to log in the console, replacing the print statement in this way will be a great help in future project maintenance.

Configuring Logging by File

If you want to manage logging through a configuration file, you can refer to this official document. This is a very common way in log4net or log4j.

# logging.conf[Loggers]keys=Root[Logger_root]Level=DEBUGhandlers=consolehandler#,timedrotatefilehandler,errortimedrotatefilehandler############################################ #####[Handlers]keys=Consolehandler,timedrotatefilehandler,errortimedrotatefilehandler[Handler_consolehandler]class=StreamhandlerLevel=DEBUGFormatter=Simpleformatterargs=(Sys.stdout,)[Handler_timedrotatefilehandler]class=Handlers. TimedrotatingfilehandlerLevel=DEBUGFormatter=Simpleformatterargs=(' Debug.log ', ' H ')[Handler_errortimedrotatefilehandler]class=Handlers. TimedrotatingfilehandlerLevel=WARNFormatter=simpleformatterargs= (' Error.log ', ' H ') #################################################[formatters] keys=simpleformatter, Multilineformatter [formatter_simpleformatter]format= % (levelname) s% (ThreadName) s% (asctime) s:% (message) Sdatefmt= %h:%m:%s[formatter_multilineformatter] format= -------------------------% (LevelName) s------------- ------------Time:% (asctime) s Thread:% (threadname) s File:% (filename) s (line% (Lineno) d) Message:% (Message) Sdatefmt=%y-%m-%d%h:%m:%s      

Assuming that the above configuration file is placed in the same directory as the module, the call in the code is as follows.

import osfilepath = os.path.join(os.path.dirname(__file__), ‘logging.conf‘)logging.config.fileConfig(filepath)return logging.getLogger()
Log repeat output Pit

You may see that the logs you hit are repeated several times, possibly for a number of reasons, but the summary is just one, and the log uses duplicate handler.

First Pit
import logginglogging.basicConfig(level=logging.DEBUG)fmt = ‘%(levelname)s:%(message)s‘console_handler = logging.StreamHandler()console_handler.setFormatter(logging.Formatter(fmt))logging.getLogger().addHandler(console_handler)logging.info(‘hello!‘)# INFO:root:hello!# INFO:hello!

The above example has a duplicate log, because basicConfig() a handler is created by default when the method is called on line 3rd, and if you add another console handler, a duplicate log appears.

Second Pit
 import loggingdef get_logger (): FMT =  "% (levelname) s:% (message) s ' Console_handler = logging. Streamhandler () Console_handler.setformatter (logging. Formatter (FMT)) logger = Logging.getlogger ( ' App ') logger.setlevel (Logging.info) Logger.addhandler (Console_handler) return loggerdef call_me (): Logger = Get_logger () Logger.info ( ' Hi ') call_me () Call_me () # info:hi# info:hi# info:hi        

In this case hi , it actually prints three times, what if we call call_me() it again? I told you it would print 6 times. Why? Because you add a new handler to the method every time you invoke get_logger() it, you deserve it. The normal practice should be to configure logger only once.

Third Pit
Import loggingDefGet_logger(): FMT ='% (levelname) s:% (message) s ' Console_handler = logging. Streamhandler () Console_handler.setformatter (logging. Formatter (FMT)) logger = Logging.getlogger ( ' App ') logger.setlevel (logging.info) Logger.addhandler (console_handler) return loggerdef  Foo (): Logging.basicconfig (Format= "[% (name) s]:% (message) s ') Logging.warn ( ' Some module use root logger ') def main (): Logger = Get_logger () logger.info ( Span class= "hljs-string" "App start." Foo () logger.info (# info:app start. # [Root]: Some module use root logger# info:app shutdown. # [app]: app shutdown.               

Why did the last App shutdown print two times? So a lot of people on the StackOverflow asked, how should I turn the root logger off, root logger too hang Dad hang mom. As long as you have used root logger in your program, it will be counted as a copy of all the logs you print by default. There is no good way to do this, and I suggest you recruit someone who uses root logger without a brain, beat him to death or expel him.

If you really want to disable root logger, there are two ways to do this:

logging.getLogger().handlers = []  # 删除所有的handlerlogging.getLogger().setLevel(logging.CRITICAL)  # 将它的级别设置到最高
Summary

The log module in Python is a part of the standard library, and the functionality is quite complete. Personal feel simple, but also support such as filtering, file lock and other advanced features, to meet the needs of most projects.

But remember, be careful with the pit.

Python's logging log module (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.