Python-Common modules-logging modules

Source: Internet
Author: User

Python primarily uses the logging module for log processing

Many programs have logging requirements, and the log contains information that has a normal program Access log, there may be errors, warnings and other information output, Python's logging module provides a standard log interface,

You can store logs in various formats through it, logging logs can be divided into debug (), info (), warning (), error (), critical () 5 levels,

Log level:
DEBUG
INFO
WARNING
ERROR
CRITICAL
Rank in turn (less information is printed, debug most detailed)

1 Log Format:2%(name) s logger name (default root)3%(Levelno) The log level in the S-word form is:4Debug:10 info:20 warning:30 error:40 critical:505%(levelname) s log level in text form6%(pathname) s The full pathname of the module that invokes the log output function, possibly without7%(filename) s The file name of the module that invokes the log output function8%module Name of the log output function called by (module) s9%(message) s user-output messagesTen%(funcName) s function name of the call log output function One%(Lineno) d The line of code where the statement that invokes the log output function A%(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 the%(thread) d thread ID. Probably not . -%(threadname) s thread name. Probably not . -% (process) d process ID. Probably not.

The Log module uses:

1, the simplest use, print to the screen (without print, bring your own printing)

1 Import Logging 2 logging.warning ('user [root] attempted wrong password more than 3times') 
    3 logging.critical ('serveris down'

#想要debug (), info (), or error () Just like above, add it.

2. Set the time

1 Import Logging 2 logging.basicconfig (format='% (asctime) s% (message) s', datefmt=' %y-%m-%d%h:%m:%s ' )3 logging.warning ("is"when thisevent was logged. ')

3. Write the log to the file

1 ImportLogging2 3Logging.basicconfig (filename='./file/logging.log', level=logging. debug,format='% (asctime) s% (name) s-% (LevelName) s->% (Levelno) s:% (message) s', datefmt='%y-%m-%d%h:%m:%s')4Logging.debug ('This message should go to the log file')5Logging.info ('So should this')6Logging.warning ('and this, too')7Logging.error ('and this, too')8Logging.critical ('and this, too')

File contents:

2017-04-04 17:14:10 root-debug->10:this message should go to the log file
2017-04-04 17:14:10 Root-info->20:so should this
2017-04-04 17:14:10 root-warning->30:and This, too
2017-04-04 17:14:10 root-error->40:and This, too
2017-04-04 17:14:10 root-critical->50:and This, too

4. Write the log at the same time to the file and print to the screen

1 " "2 Python uses the logging module to log logs involving four main classes, which are best used in the official documentation:3 4 logger provides an interface that the application can use directly;5 Logger.setlevel (LEL): Specifies the lowest log level, and the level below LEL is ignored. Debug is the lowest built-in level, critical is the highest6 Logger.addfilter (filt), Logger.removefilter (filt): Add or remove the specified filter7 Logger.addhandler (HDLR), Logger.removehandler (HDLR): Add or remove the specified handler8 logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical (): Log levels you can set9 Ten handler sends the log record (created by logger) to the appropriate destination output; One Handler.setlevel (LEL): Specifies the level of information being processed, and information below the LEL level is ignored A handler.setformatter (): Choose a format for this Handler - Handler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter object -  the Filter provides a fine-grained device to determine which log record to output; -  - Formatter determines the final output format of the log record.  - " " + ImportLogging -  + #Create Logger ALogger = Logging.getlogger ('Xt-log') at Logger.setlevel (logging. DEBUG) -      #set the global log level (global precedence is higher than local but local if local to global) -       - #Create a console handler, and set its log level to debug -CH =logging. Streamhandler () - Ch.setlevel (logging. DEBUG) in  - #Create a file handler, and set its log level to warning toFH = logging. Filehandler ('./file/logging.log') + Fh.setlevel (logging. WARNING) -  the #Create a formatter (format) *Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s', datefmt='%y-%m-%d%h:%m:%s') $ Panax Notoginseng #Add formatter to console handler and file handler - Ch.setformatter (Formatter) the Fh.setformatter (Formatter) +  A #Add Console handler and file handler to logger the logger.addhandler (CH) + Logger.addhandler (FH) -  $ #setting error Messages $Logger.debug ('Debug Message') -Logger.info ('Info Message') -Logger.warning ('warning Message') theLogger.error ('error Message') -Logger.critical ('Critical Message')

Above output to screen result:

2017-04-08 16:12:49-xt-log-debug-debug Message
2017-04-08 16:12:49-xt-log-info-info Message
2017-04-08 16:12:49-xt-log-warning-warning Message
2017-04-08 16:12:49-xt-log-error-error Message
2017-04-08 16:12:49-xt-log-critical-critical Message

Python-Common modules-logging modules

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.