Python module-logging's IQ limit

Source: Internet
Author: User

Logging, so the name wanton is the log, I 艹, this cultural heritage!

Logging is a python built-in log module that facilitates log writing and output for everyday applications

The logging is divided into 5 log levels, namely:

Debug, info, warning, error, critical (ranked by order)

where info is the log level that is often applied in the future, for logging, the default level is warning, in other words, is equal to or higher than warning level, will be output, the default warning level, will output Warning,error, Critical rating

code example:

1 #edited by: Shanlong2 ImportLogging3Logging.debug ("Debug")4Logging.info ("Info")#the most commonly used output level5Logging.warning ("Warning")6Logging.error ("Error")7Logging.critical ("Critical")8 9 #Printing results:Ten #WARNING:root:warning One #ERROR:root:error A #CRITICAL:root:critical

So what about the logging log output format, and the logging level configuration?

There are two ways to operate in logging, the first of which is Basicconfig, and the second is logger

So what's the difference between these two ways? Each of the following is resolved:

The first way, Baseconfig:

code example:

1 #edited by: Shanlong2 ImportLogging3 Logging.basicconfig (4Level=logging. DEBUG,#The default level for change logs is debug5format="% (asctime) s% (message) s",6     #formats in format can be viewed in the description document of the Basicconfig7datefmt="%y-%m-%d%h:%m:%s",8     #time format, seconds originally%s but in string%s is a placeholder, so you must use%s to resolve the conflict .9Filename="Test.log",#log FileTenFilemode="a"#file operation method is append write One ) A  -Logging.debug ("Debug Message") -Logging.info ("Info")#the most commonly used output level theLogging.warning ("Warning") -Logging.error ("Error") -Logging.critical ("Critical") -  + #Test.log: - #2017-04-30 18:41:23 Debug Message + #2017-04-30 18:41:23 Info A #2017-04-30 18:41:23 Warning at #2017-04-30 18:41:23 Error - #2017-04-30 18:41:23 Critical

Its advantages are simple and easy to operate, but a lot of shortcomings, such as the inability to output screen and file together, so there is a second way

The second way, logger: Common Log configuration methods

code example:

1 #edited by: Shanlong2 ImportLogging3Logger = Logging.getlogger ()#instantiate a Logger object4FH = logging. Filehandler ("Logger2")#instantiate a file stream object FH5SH = logging. Streamhandler ()#instantiate a stream stream object sh6FM = logging. Formatter ("% (asctime) s-% (message) s")7 #instantiate a log Format object, FM8 fh.setformatter (FM)9 #Add the Log Format Object FM to the FH object so that the log is formatted as defined in FM when the file is outputTen Logger.addhandler (FH) One #Add the file stream object FH to the Logger object so that the log output can be a file stream mode A Logger.addhandler (SH) - #Add the file stream object FH to the Logger object so that the log output can be a stream stream -Logger.debug ("Debug Message") theLogger.info ("Info")#the most commonly used output level -Logger.warning ("Warning") -Logger.error ("Error") -Logger.critical ("Critical") +  - #Logger2: + #2017-04-30 18:53:22,324-warning A #2017-04-30 18:53:22,324-error at #2017-04-30 18:53:22,324-critical - #2017-04-30 18:57:53,341-warning - #2017-04-30 18:57:53,341-error - #2017-04-30 18:57:53,342-critical - #Printing results: - #Warning in #Error - #Critical to #Warning + #Error - #Critical

According to Mr. Yuan's theory, logger configuration, like a star-sucking Dafa, is a suck-up process, what do you have I suck at, this way really well understood

Logger way in the future we want to use often, so be sure to remember OH

Python module-logging's IQ limit

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.