Python's logging module

Source: Internet
Author: User

Reference: http://blog.csdn.net/zyz511919766/article/details/25136485

Logging module

The log has 5 levels, debug,info,warning,error,critical, respectively, and the corresponding level numbers are 10,20,30,40,50

Output log standards to the screen

#!/usr/bin/env python#-*-coding:utf-8-*-ImportLogginglogging.debug ("Welcome to Akon Debug System") Logging.info ("Welcome to Akon Info System") logging.warning ("Welcome to Akon Warning System") Logging.error ("Welcome to Akon Error System") logging.critical ("Welcome to Akon critical system")

Output Result:

WARNING:root:welcome to Akon WARNING SystemERROR:root:welcome to Akon error SystemCRITICAL:root:welcome to Akon critical s Ystem

The results are visible, and by default only logs that are equal to or higher than the warning level are printed.

Save the log in a custom format to a file

#!/usr/bin/env python#-*-coding:utf-8-*-ImportLogginglogging.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='Access.log', FileMode='a') Logging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message')

View Access.log file Contents:

Tue, 17:00:42 log.py[line:18(17:00:42 log.py[line:19) 17:00:42 log.py[l Ine:2017:00:42 log.py[line:2117:00:42 log.py[line:22] CRITICAL CRITICAL message

Use the Logging.baseconfig () function to define the log output format with the following parameters:

FileName: Creates a filedhandler with the specified filename 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 used by handler. DATEFMT: Specifies the date time format. Level: Set Rootlogger (The following explains the specific concept) stream: Create 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.

formatting strings that may be used in the format parameter:

%% (Levelno) s The log level in digital form %% ( Pathname) s The full pathname of the module that called the log output function (may not have) % (filename) s The file name of the module that called the log output function % (module) s the module name of the call log output function % (FuncName) s Call the function name of the log output function % (Lineno) d the line where the statement of the log output function is called %% Span style= "color: #000000;" > (relativecreated) d when outputting log information, the current time as a string of % (asctime) s as the number of milliseconds since logger was created. The default format is "2003-07-08 16:49:45,896". The comma is followed by the milliseconds % "D thread ID (possibly not) % ( ThreadName) s thread name (may not have) % (process) d process ID (may not) % (message) s user output messages 

Output logs to the screen and saved to a file at the same time

#!/usr/bin/env python#-*-coding:utf-8-*-ImportLogging#Create LoggerLogger = Logging.getlogger ("MyLog") Logger.setlevel (logging. DEBUG)#Create formatter log output formatFormatter = logging. Formatter (Format ("% (asctime) s% (name) s% (levelname) s% (message) s"), Datefmt="%a%d%b%y,%h:%m:%s")#Create console handler and set level to debugCH =logging. Streamhandler () ch.setlevel (logging.info)#create file handler and set level to debugFH = logging. Filehandler ("Mytestlog.log") Fh.setlevel (logging. WARNING)#the Add formatter to CH and FH assigns the output format to CH and FHCh.setformatter (Formatter) fh.setformatter (formatter)#Add ch and fh to loggerlogger.addhandler (CH) logger.addhandler (FH) Logger.debug ("{} Debug Message". Format ("Akon")) Logger.info ("{} info message". Format ("Akon")) logger.warning ("{} warning message". Format ("Akon")) Logger.error ("{} error message". Format ("Akon")) logger.critical ("{} Critical message". Format ("Akon"))

The screen output is:

Tue Mar 2016,18:18:362016,18:18:36Mar 2016,18:18:36April Mar 2016,18:18:36 mylog CRI Tical Akon Critical Message

Save the file as:

Tue Mar 2016,18:18:362016,18:18:36Mar 2016,18:18:36 mylog CRITICAL Akon CRITICAL Message

Python's logging module

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.