Python Module logging OS commands

Source: Internet
Author: User

Log for the program to run and technical personnel is very necessary and very important, troubleshooting is generally from the analysis program to run the log, and then the complexity of the large program must have a log input, otherwise, even if the program is not qualified. Python provides a handy logging module for technicians to define and output logs.

Let's take a look at the log level and the simple output of logging, as in the following example:

 #导入日志模块logging 
Import Logging
# outputs different levels of log
Logging.debug (' This is debug Info ')
Logging.info (' This is Information ')
Logging.warn (' This is warning message ')
Logging.error (' This is the error message ')
Logging.fatal (' This was fatal message, it is same as logger.critical ')
logging.critical (' Thi S is critical message ')

Run Result:
WARNING:root:This is WARNING message
ERROR:root:This is ERROR message
CRITICAL:root:This is fatal message, it's same as logger.critical
CRITICAL:root:This is CRITICAL message

As you can see from the example above, Python's logging module prints the logs to standard output and only displays logs that are greater than or equal to the warning level, indicating that the default logging level is set to warning (log level level critical > ERROR > WARNING > INFO > DEBUG > NOTSET), the default log format is log level: Logger name ( the default logger name is root): User output message.

We can customize the output log format, location, and level through the Basicconfig function of the logging module, as in the following example:

#basicConfig定义日志级别, log format, output location
Import logging
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',
Datefmt= '%a,%d%b%Y%h:%m:%s ',
Filename= ' Test.log ',
Filemode= ' W ')
Logging.debug (' Debug message ')
Logging.info (' info message ')
Logging.warning (' warning message ')
Logging.error (' Error message ')
Logging.critical (' critical message ')

Operation Result:
Cat Test.log
Thu, 10:03:44 rizhi.py[line:20] Debug Debug message
Thu, 10:03:44 rizhi.py[line:21] Info Info message
Thu, 10:03:44 rizhi.py[line:22] WARNING WARNING message
Thu, 10:03:44 rizhi.py[line:23] Error error message
Thu, 10:03:44 rizhi.py[line:24] CRITICAL CRITICAL message

As seen from the example, the default behavior of the logging module can be changed by specific parameters in the Logging.basicconfig () function, the available parameters are:
FileName: With the specified file name, so that the log is stored in the specified file.
FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W".
Format: Specifies the log display format to use.
DATEFMT: Specifies the date time format.
Level: Log levels set
Stream: Creates a streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or to a file, and the default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored.
One of the formatting strings that may be used in the format parameter:
% (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 the time
% (relativecreated) d when the log information is output, 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

the log output provided by the Logging.basicconfig () function is flexible enough, but there is another more flexible module-level function Logging.getlogger (), which returns a Logger object, If no name is specified, the root logger will be returned. To understand this function, we need to first understand the concept of logger,handler,formatter,filter.

The Logger object provides an interface that the application can use directly, handler sends logs to the appropriate destination, and filter provides a way to filter the log information formatter specify the log display format.

Example: Demo logging.getlogger () usage

Import logging
# Create a Logger
Logger = Logging.getlogger ()
Logger1 = Logging.getlogger (' MyLogger ')
Logger1.setlevel (logging. DEBUG)
Logger2 = Logging.getlogger (' MyLogger ')
Logger2.setlevel (Logging.info)
Logger3 = Logging.getlogger (' mylogger.child1 ')
Logger3.setlevel (logging. WARNING)
# Create a handler to write to the log file
FH = logging. Filehandler (' Test.log ')
# Create another handler for output to the console
ch = logging. Streamhandler ()
# define the output format of the handler formatter
Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
Fh.setformatter (Formatter)
Ch.setformatter (Formatter)
# define a filter
Filter1 = logging. Filter (' MyLogger ')
Filter2 = logging. Filter (' Mylogger.child1 ')
# Fh.addfilter (Filter)
# Add Handler to Logger
Logger.addhandler (FH)
Logger.addhandler (CH)
Logger1.addfilter (Filter1)
Logger1.addhandler (FH)
Logger1.addhandler (CH)
Logger2.addfilter (Filter1)
Logger2.addhandler (FH)
Logger2.addhandler (CH)
Logger3.addfilter (Filter2)
Logger3.addhandler (FH)
Logger3.addhandler (CH)
# Record a log
Logger.debug (' Logger debug message ')
Logger.info (' Logger info message ')
Logger.warning (' Logger warning message ')
Logger.error (' Logger error message ')
Logger.critical (' Logger critical message ')
Logger1.debug (' logger1 debug Message ')
Logger1.info (' logger1 info message ')
Logger1.warning (' logger1 warning message ')
Logger1.error (' logger1 error message ')
Logger1.critical (' logger1 critical message ')
Logger2.debug (' logger2 debug Message ')
Logger2.info (' logger2 info message ')
Logger2.warning (' logger2 warning message ')
Logger2.error (' logger2 error message ')
Logger2.critical (' logger2 critical message ')
Logger3.debug (' logger3 debug Message ')
Logger3.info (' logger3 info message ')
Logger3.warning (' logger3 warning message ')
Logger3.error (' logger3 error message ')
Logger3.critical (' logger3 critical message ')

Operation Result:
2017-11-09 22:51:59,654-root-warning-logger WARNING Message
2017-11-09 22:51:59,654-root-error-logger ERROR Message
2017-11-09 22:51:59,654-root-critical-logger CRITICAL Message
2017-11-09 22:51:59,655-mylogger-info-logger1 INFO Message
2017-11-09 22:51:59,655-mylogger-info-logger1 INFO Message
2017-11-09 22:51:59,655-mylogger-warning-logger1 WARNING Message
2017-11-09 22:51:59,655-mylogger-warning-logger1 WARNING Message
2017-11-09 22:51:59,655-mylogger-error-logger1 ERROR Message
2017-11-09 22:51:59,655-mylogger-error-logger1 ERROR Message
2017-11-09 22:51:59,655-mylogger-critical-logger1 CRITICAL Message
2017-11-09 22:51:59,655-mylogger-critical-logger1 CRITICAL Message
2017-11-09 22:51:59,655-mylogger-info-logger2 INFO Message
2017-11-09 22:51:59,655-mylogger-info-logger2 INFO Message
2017-11-09 22:51:59,655-mylogger-warning-logger2 WARNING Message
2017-11-09 22:51:59,655-mylogger-warning-logger2 WARNING Message
2017-11-09 22:51:59,655-mylogger-error-logger2 ERROR Message
2017-11-09 22:51:59,655-mylogger-error-logger2 ERROR Message
2017-11-09 22:51:59,655-mylogger-critical-logger2 CRITICAL Message
2017-11-09 22:51:59,655-mylogger-critical-logger2 CRITICAL Message
2017-11-09 22:51:59,657-mylogger.child1-warning-logger3 WARNING Message
2017-11-09 22:51:59,657-mylogger.child1-warning-logger3 WARNING Message
2017-11-09 22:51:59,657-mylogger.child1-warning-logger3 WARNING Message
2017-11-09 22:51:59,657-mylogger.child1-error-logger3 ERROR Message
2017-11-09 22:51:59,657-mylogger.child1-error-logger3 ERROR Message
2017-11-09 22:51:59,657-mylogger.child1-error-logger3 ERROR Message
2017-11-09 22:51:59,657-mylogger.child1-critical-logger3 CRITICAL Message
2017-11-09 22:51:59,657-mylogger.child1-critical-logger3 CRITICAL Message
2017-11-09 22:51:59,657-mylogger.child1-critical-logger3 CRITICAL Message

logger = Logging.getlogger () returns a default logger, root logger, and applies the default log level, handler, and formatter settings. You can also specify the lowest log level through Logger.setlevel (LEL), which has logging available log levels. DEBUG, Logging.info, logging. WARNING, logging. ERROR, logging. CRITICAL.

Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical () output different levels of logging, Only logs with log levels greater than or equal to the set log level will be output.

(1) From the above example can be found through logger1.setlevel (logging. Debug) Sets the log level of the Logger1 to debug, but does not output the debug level of log information, but is displayed from the log at the info level, because Logger1 and logger2 correspond to the same logger instance MyLogger, The logger instance returned is the same as long as the name parameter in Logging.getlogger (name) is the same, and only one, that is, name corresponds to logger instance one by one. The log level for MyLogger is set to Logging.info in the Logger2 instance through Logger2.setlevel (Logging.info), so the output of the last Logger1 follows the log level set later.

Logger1 = Logging.getlogger (' MyLogger ')
Logger1.setlevel (logging. DEBUG)
Logger2 = Logging.getlogger (' MyLogger ')
Logger2.setlevel (Logging.info)

(2) From the example output can be seen logger1, logger2 corresponding to each output display two times, logger3 corresponding output display 3 times, this is because the Logger = Logging.getlogger () display of the creation of the root Logger, Logger1 = Logging.getlogger (' MyLogger ') creates a subclass of root logger (root). MyLogger, and so on, logger2 the same.

Logger3 = Logging.getlogger (' mylogger.child1 ')
Logger3.setlevel (logging. WARNING)

(3)Formatter object Sets the last rule, structure, and content of the log information, the default time format is%y-%m-%d%h:%m:%s.

Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
Fh.setformatter (Formatter)
Ch.setformatter (Formatter)

(4) filter limit only logs that satisfy the filtering rules will be output. For example, we define filter = logging. Filter (' A.B.C '), and add this filter to a handler, then only A.B.C with the logger prefix in the logger of the handler will be able to output its log.

Filter1 = logging. Filter (' MyLogger ')
Filter2 = logging. Filter (' Mylogger.child1 ')



This article is from the "Dreamscape" blog, make sure to keep this source http://dyqd2011.blog.51cto.com/3201444/1980460

Python Module logging OS commands

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