Python Road 36-logging Module

Source: Internet
Author: User

#很多程序都有记录日志的需求, and the information contained in the log is a normal program access log, there may be errors, warnings and other information output, Python logging module provides a standard log interface, you can store the log in various formats, logging log can be divided into  debug (),  info (),  warning (),  error ()  and critical ()  5 levels import logging #打印日志logging. Warning ("User [alex] attempted wrong password more than 3  times ") logging.critical (" Server is down ") #把日志写到文件中 # Level=loggin in the following sentence. Info means that the log record level is set to info, that is, only logs that are more than the log is info or higher than the info will be recorded in the file, in this example,  the first log is not recorded, if you want to record the debug log, Change the log level to debug on the line. Logging.basicconfig (filename= "Example.log", Level=logging.info) logging.debug ("this is debug  Message ") Logging.info (" This is info message ") logging.warning (" this is warning  Message ") #给日志加上时间logging. Basicconfig (filename=" Example.log ", level=logging.info,format="% (asctime) s % ( Message) S ", datefmt="%y-%m-%d %h:%m:%s ") logging.warning (" This is warning message ") #  Log format # % (name) S# logger name # % (Levelno) s#  the log level in digital form # % (levelname) s#  the log level in text Form # % (pathname) s#  The full pathname of the module that called the log output function, The module name of the module that may not have # % (filename) s#  called the log output function # % (module) s#  call the log output function of the modules # % (funcName) s#  Call the function name of the log output function # % (Lineno) d#  the code line where the statement that called the log output function # % (created) f#  the current time, represented by the UNIX standard float   number of time  % (relativecreated) d#  output log information, the number of milliseconds since logger was created in   to # % (asctime) s#  the current time in string form. The default format is   "2003-07-08 16:49:45,896". The comma is followed by the millisecond # % (thread) d#  thread ID. There may not be a # % (threadname) s#  thread name. There may not be a # % (process) d#  process ID. There may be no # % (message) s#  user output message Logging.basicconfig (filename= "Example.log",                     level=logging.info,                      format= "% (asctime) s % (levelname) s - % (message) s",                      datefmt= "%Y-%m-%d  %h:%m:%s ") logging.debug (" This is debug ") logging.info (" This is info ") logging.warning ( "This is warning") logging.error ("This is error") logging.critical ("This is  Critical ") #将日志分别以文件和控制台形式分别输出 # create loggerlogger = logging.getlogger (' Test-log ') Logger.setlevel (logging. DEBUG) # create console handler and set level to debugch =  Logging. Streamhandler () Ch.setlevel (logging. DEBUG) # create file handler and set level to warningfh =  Logging. Filehandler ("Access.log") fh.setlevel (logging. WARNING) # create formatterformatter = logging. Formatter (fmt= '% (asctime) s - % (name) s - % (levelname) s - % (message) s ', datefmt= "% y-%m-%d %h:%m:%s ") # add formatter to ch&nBsp;and fhch.setformatter (Formatter) fh.setformatter (formatter) # add ch and fh to  loggerlogger.addhandler (CH) logger.addhandler (FH) #  ' Application '  codelogger.debug (' debug  Message ') logger.info (' Info message ') logger.warn (' Warn message ') logger.error (' Error message ') Logger.critical (' critical message ') #截断日志import  loggingfrom logging import  Handlerslogger = logging.getlogger (__name__) log_file =  "Timelog.log" #fh  =  Handlers. Rotatingfilehandler (filename=log_file,maxbytes=10,backupcount=3) fh = handlers. Timedrotatingfilehandler (filename=log_file,when= "D", interval=1,backupcount=5) #filename   log file #when     at what time truncation, days, hours, minutes, seconds #interval  interval #backupcount     keep several backups formatter =  logging. Formatter (fmt= '% (asctime) s % (module) s:% (Lineno) d % (message) s ',                                datefmt= "%y-%m-%d %h:%m:%s") Fh.setformatter (Formatter) logger.addhandler (FH) logger.warning ("Test1") Logger.warning ("test12") logger.warning ("test13") logger.warning ("test14")


This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1897629

Python Road 36-logging 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: 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.