Python Automation Road Logging log Module

Source: Internet
Author: User
Tags set time

Logging Log Module

Http:// Chinese official Clear, essential for getting Started inheritance is great. Example Analysis

One: Overview

In a real project, you need to log some data and log it to a different storage unit, such as a database, text, or push to a graphical interface, and when needed, find yourself implementing a day
The library is really a big price, so the third party's log library on the custom processing body content is the logging understanding and use of the way, very convenient
1: Four main classes, using the summary in the official documentation:
Logger provides an interface that the application can use directly;
handler sends the log record (created by logger) to the appropriate destination output;
Filter provides a fine-grained device to determine which log record to output; not very useful.
Formatter Determining the final output format of the log record
2: module-level functions
Logging.getlogger ([name]): #返回一个logger对象, if no name is specified will return root logger, most commonly
Logging.basicconfig (): #给logger对象的配置管理函数, infrequently used
Logging.debug (), (),logging.warning (),logging.error (), Logging.critical (): #logger的日志级别

II: Logging Workflow Demo

#coding: Utf-8
Import logging
# Create a logger named MyLogger,% (name) s can call this name
MyLogger =Logging.getlogger(' MyLogger ')
Mylogger.setlevel (logging. DEBUG)
# define log Output format formatter
Formatter =logging. Formatter('% (asctime) s-% (name) s-% (filename) s-% (levelname) s-% (message) s ')
# Create a handler to write to the log file, output only logs above the debug level, and invoke the defined output format
FH =logging. Filehandler(' Test.log ')
Fh.SetLevel (logging. DEBUG)
Fh.Setformatter (Formatter)
# Create another handler for output to the console, typically not
CH =logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
Ch.setformatter (Formatter)
# give us a logger object to start instantiating add handler
Mylogger.addhandler (FH)
Mylogger.addhandler (CH)
# Call log two logs directly in this module--production environments are encapsulated into function calls' Foorbar ')
Mylogger.debug (' Just a test ')
$ python
2015-10-30 15:44:01,
2015-10-30 15:44:01, a test examples

Three: API for logging module

1:logging.getlogger ([name])
Returns a logger instance that returns root logger if no name is specified. As long as name is the same, the returned logger instances are the same and only one, that is, the name and logger instances are a pair
should be. This means that there is no need to pass logger instances in each module. As long as you know name, you can get the same logger instance
2:logger.setlevel (LVL)--Set the level of logger,
Level has the following levels:
Digital control corresponding to different log levels
Level values
If the level of logger is set to info, then logs less than the info level are not output, and logs greater than or equal to the info level are output. It also means that the same logger instance, if multiple places are called, will
A lot of duplicate logs appear
Logger. Debug("Debug") # does not output
Logger. Info("info") # output
Logger. Warning("Warning") # output
Logger. Error("error") # output
Logger. Critical("critical") # output
3:logger.addhandler (HD)--Logger hired handler to help it with the logs.
The handler object is responsible for sending relevant information to the specified destination. Python's log system can be used in a variety of handler. Some handler can output information to the console, and some logger can
Out to the file, and some handler can send the information to the network. If you feel that it is not enough, you can write your own handler. Multiple handler can be added through the AddHandler () method
Handler has the following main types:
logging. Streamhandler:#日志输出到流即控制台, it could be Sys.stderr, sys.stdout.
logging. Filehandler:#日志输出到文件
Logging.handlers.RotatingFileHandler#日志输出到文件 and cut according to the log file size set
Logging.handlers.TimedRotatingFileHandler#日志输出到文件, and cut the log file at set time
Logging.handlers.SocketHandler:#远程输出日志到TCP/IP Sockets
Logging.handlers.DatagramHandler:#远程输出日志到UDP sockets
Logging.handlers.NTEventLogHandler:Event Log for #远程输出日志到Windows NT/2000/XP
BecauseStreamhandlerAndFilehandleris a common method of log processing, so it is included directly in theLoggingmodule, and other methods are included in thelogging.handlersmodule,
handle Common calls
Handler. SetLevel (LEL)#指定被处理的信息级别, information below the LEL level is ignored
Handler. Setformatter () #Choose a format for this handler
Handler.addfilter(filt), Handler. removefilter(filt): #新增或删除一个filter对象
raise two chestnuts .
1:logging Internal Call--Test learning can be
Import logging
Import Logging.handlers
log_file = ' Api.log '
# define log file cutting rules backup up to 5 log files, maximum 10M per log file
Handler =Logging.handlers.RotatingFileHandler (log_file, MaxBytes = 10*1024*1024, Backupcount = 5)
#handler =logging.handlers.timedrotatingfilehandler (Log_file, when= ' Midnight ') #每天零点切换
# define Log Output format
FMT = '% (asctime) s-% (filename) s:% (lineno) s-% (name) s-% (message) s '
Formatter = logging. Formatter (FMT) # instantiation Formatter
Handler.setformatter (Formatter) # Add formatter for Handler
# Instantiate a Logger object and bind it handle
MyLogger = Logging.getlogger (' Test ')
Mylogger.addhandler (handler) # Add handler for MyLogger
Mylogger.setlevel (logging. DEBUG) # Set output level for MyLogger
#调用mylogger (' first info message ')
Mylogger.debug (' first debug message ')
2:logging Encapsulation as a function--for production environments
#/usr/bin/env python
#coding: Utf-8
Import Logging,logging.handlers
def writelog (log_name):
Log_filename = "/tmp/test.log"
Log_level = logging. DEBUG
format =Logging. Formatter ('% (asctime) s% (filename) s [line:% (Lineno) 2d]-% (funcName) s% (levelname) s% (message) s ')
handler =Logging.handlers.RotatingFileHandler (Log_filename, mode= ' A ', maxbytes=10*1024*1024, backupcount=5)
handler.setformatter (format)
logger = Logging.getlogger (log_name)
Logger.addhandler (handler)
logger.setlevel (log_level)
Return Logger#函数最终将实例化的logger对象返回, which can be called directly after
if __name__ = = "__main__":
writelog (' API '). Info (' 123 ')#模块内部直接调用函数. Equivalent to the following two lines, the following method is not recommended
# writelog = writelog (' API ')
# (' 123 ')
outside the program call function
Import Util
def index ()
util. Writelog (' API '). Info (' 123 ')
Index ()
4:logging.basicconfig ([**kwargs])--load the logger configuration parameters, not good
Import logging
Logging.basicconfig(level=logging. DEBUG,#输出debug及其级别更高级别的日志
format= '% (asctime) s% (filename) s [line:% (Lineno) d]% (levelname) s% (message) s ',
Datefmt= '%d%b%Y%h:%m:%s ',
filename= ' Myapp.log ',#日志文件输出的文件地址, do not write default print to desktop
filemde= ' W ')
Logging.debug ("This is Debug message") ("This is Info message")
Logging.warning ("This is warning message")
Tail-f Myapp.log
OCT 14:18:51 [Line:8] Debug this is debug message
OCT 14:18:51 [Line:9] Info This is INFO message
OCT 14:18:51 [line:10] WARNING this is WARNING message
general configuration about the Logging.basicconfig function:
FileName:Specify the log file name
FileMode:Same as file function, specify open mode of log file, ' W ' or ' a '
datefmt:Specify time format, same as Time.strftime ()
Level :Set the logging level, which defaults to logging. WARNING, that is, WARNING and higher-level logs are output
Stream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr,When stream and filename are specified at the same time, Stream is
format:Specifies the format and content of the output, and format can output a lot of useful information, as shown in the previous example:
% (name) sPrint logger name, default to root
% (Levelno) s:Print the value of the log level
% (levelname) s: Print Log level name
% (pathname) s:Prints the path to the current executable, which is actually sys.argv[0]
% (filename) s:Print the current execution program name
% (funcName) s:Print the current function of the log
% (Lineno) d:Print the current line number of the log
% (asctime) s:Time to print the log
% (message) s: Print log information
% (thread) d: Print thread ID
% (threadname) s: Print thread name
% (process) d: Print process ID
5:logging.configThe module is loaded by the way the configuration fileLoggerThe parameters--The best way to use it
Cat logger.conf
# define the Logger module, root is the parent class, must exist, others are custom.
# Logging.getlogger (NAME) is equivalent to registering an instantiation with the logging module
# name. Represents the inheritance relationship of log
# [LOGGER_XXXX] Logger_ module name
# level with Debug, INFO, WARNING, ERROR, CRITICAL
# Handlers processing class, can have multiple, separated by commas
# Qualname Logger name, the application gets through Logging.getlogger. For names that cannot be obtained, the root module is logged.
# Propagate whether to inherit the log information of the parent class, 0: No 1: Yes
# class Handler classes name
# level log Levels
# Formatter, the formatter defined above
# args Handler initialization function arguments
Args= (Sys.stderr,)
args= (' Myapp.log ', ' a ')
Class=handlers. Rotatingfilehandler
args= (' Myapp.log ', ' a ', 10*1024*1024, 5)
# log Format
format=% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s
format=% (asctime) s% (name) -12s:% (levelname) -8s% (message) s
Import logging
Import Logging.config
Logging.config.fileConfig ("logger.conf")
Logger = Logging.getlogger ("example01")
Logger.debug (' This is Debug message ') (' This is Info message ')
Logger.warning (' This is warning message ')
Use cases in a production environment-call through function definitions
A: defined in the tool module
Import Logging,
Import Logging.config
def write_log (loggername):
Work_dir = Os.path.dirname (Os.path.realpath (__file__))
log_conf= os.path.join (Work_dir, ' conf/logger.conf ')
logging.config.fileConfig (log_conf)
logger = Logging.getlogger (loggername)
Return Logger
B: External module invocation
Import Util
Util.write_log (' API '). Info (' Just a test ')
Four:AboutRoot LoggerAndLoggerThe parent-child relationship
Logger instances also have a parent-child relationship, and root logger is the topmost logger, which is the ancestor of all logger. Such as:
How to get root logger
Root logger is the default logger if you do not create a logger instance, call Logging.debug (), () logging.warning (), Logging.error (), Logging.critical () This
Some functions,
Then the logger used is root logger, which can be created automatically and is a single instance.
Log level of root logger
Root logger The default level is logging. WARNING
How to represent a parent-child relationship
The naming of the logger name can represent a parent-child relationship between logger. Like what:
Parent_logger = Logging.getlogger (' foo ')
Child_logger = Logging.getlogger (' ')
What is effective level
Logger has a concept called effective level. If a logger does not display the level, then it
With the father's level. If the father does not display the level, just use the father's level to push ....
At the end of the root logger, you must set the level. The default is logging. WARNING
After the child loggers gets the message, both the message is distributed to its handler processing, which is also passed to all ancestors logger processing,
Import logging
# set Root logger, ancestor
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 a 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 (' ')
C.debug (' foo ')
2011-08-31 21:04:29,893-foo # Father deal
2011-08-31 21:04:29,893-debug-foo # ancestor Processing
Visible, childLoggerWithout anyHandler, so the message is not processed. But it forwarded the message to its father andRoot Logger。 Output two logs at the end of the line.
There is a problem with the same log that repeats the output
Solution Solutions
1: Each logger instance gives a separate name, the output does not affect each other,
2:logging.conf in the definition does not inherit

Python Automation Road Logging log Module

Related Article

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: 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.