Python's common modules

Source: Internet
Author: User

1.logging Module

Level of the 1.1logging module

Debug debugging
Info record
Warning potential hazards
Error errors
Critical serious.

1.2 Output logs in a file

Importlogginglogging.basicconfig (filename='Log.test.log', level=logging.info,#it will only be displayed if it is higher than the set level or as a set level????format='% (asctime) s% (message) s', Datefmt='%yy-%m-%d%i:%m:%s%p') Logging.info ('My name is Gao Hui') Logging.debug ('My name is Hong Yan') logging.warning ('My name is Alex Li')

1.3 Output logs simultaneously in files and screens

ImportLogging#1. Generating Logger objectsLogger = Logging.getlogger ("Gao")#Create a Log objectLogger.setlevel (Logging.info)#set global level#2. Generate Hanlderconsole = logging. Streamhandler ()#logs are output in a screen formatConsole.setlevel (logging. WARNING)#Set Screen level#Text = logging. Filehandler ("Gao.log") #日志以文件格式输出Text.setlevel (logging. WARNING)#Set File Level#2.1 Bind the handle object to the loggerLogger.addhandler (console) Logger.addhandler (text)#3. Generating Formatter ObjectsConsole_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') File_formatter= Logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') Console.setformatter (console_formatter) text.setformatter (file_formatter) logger.warning ("test1") Logger.info ("test2")

1.4 Filtering with Filter

ImportLogging fromLoggingImporthandlersclassIgnorebackuplogfilter (logging. Filter):" "ignore logs with DB backup" "    defFilter (self, record):#fixed notation        return "DB Backup" inchRecord.getmessage ()#If there is DB backup in the log, then filter, no log is displayed#1. Generating Logger objectsLogger = Logging.getlogger ("Gao")#Create a Log objectLogger.setlevel (Logging.info)#set global level#2. Generate Hanlderconsole = logging. Streamhandler ()#logs are output in a screen formatConsole.setlevel (logging. WARNING)#Set Screen level#Text = logging. Filehandler ("Gao.log") #日志以文件格式输出Text.setlevel (logging. WARNING)#Set File Level#2.1 Bind the handle object to the loggerLogger.addhandler (console) Logger.addhandler (text)#3. Generating Formatter ObjectsConsole_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') File_formatter= Logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') Console.setformatter (console_formatter) text.setformatter (file_formatter) logger.warning ("test1") Logger.info ("test2") logging.warning ("####")#If there is DB Backup in XXXX, then filter, no log is displayed

1.5 Rotatingfilehandler

When the file reaches a certain size, it automatically renames the current log file and then creates a new log file with the same name to continue the output. When the chat.log reaches the specified size, Rotatingfilehandler automatically renames the file to Chat.log.1. However, if Chat.log1 is renamed to Chat.log2, the last re-creation Chat.log continues to output the log condition. Its functions are:

Rotatingfilehandler (filename [, mode[, MaxBytes [, Backupcount]])

MaxBytes is used to make the maximum file size for the log file. If MaxBytes is 0, it means that the log file can be infinitely large, and the renaming process described above does not occur.

The backcount is used to specify the number of reserved backup files. For example, if you specify 2, but the rename process described above occurs, the original chat.log.2 is not renamed, but is deleted.

ImportLogging fromLoggingImporthandlers#1. Generating Logger objectsLogger = Logging.getlogger ("Gao")#Create a Log objectLogger.setlevel (Logging.info)#set global level#2. Generate Hanlderconsole = logging. Streamhandler ()#logs are output in a screen formatConsole.setlevel (logging. WARNING)#Set Screen leveltext= Handlers. Rotatingfilehandler ("Gao.log", maxbytes=3,backupcount=6)#bytes, log countText.setlevel (logging. WARNING)#Set File Level#2.1 Bind the handle object to the loggerLogger.addhandler (console) Logger.addhandler (text)#3. Generating Formatter ObjectsConsole_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') File_formatter= Logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') Console.setformatter (console_formatter) text.setformatter (file_formatter) logger.warning ("test1") Logger.info ("test2")

1.6 Timerotatingfilehandler

A new log file is created automatically at a certain time interval. The process of renaming is similar to Rotatingfilehandler, but the new file is not an appended number but the current time. Its functions are:

Rotatingfilehandler (filename[, when [, interval [, Backupcount]]

where the filename parameter and the Backupcount parameter and the Rotatingfilehandler have the same meaning.

Interval is the time interval

The When parameter is a string. A unit that represents a time interval, is case-insensitive, and has the following values:

s S

M min

H hours

D Day

W per week (interval = 0 o'clock stands for Monday)

Midnight every morning

ImportLogging fromLoggingImporthandlers#1. Generating Logger objectsLogger = Logging.getlogger ("Gao")#Create a Log objectLogger.setlevel (Logging.info)#set global level#2. Generate Hanlderconsole = logging. Streamhandler ()#logs are output in a screen formatConsole.setlevel (logging. WARNING)#Set Screen leveltext= Handlers. Timedrotatingfilehandler ("Gao.log", when="Ten", Interval=5, backupcount=6) Text.setlevel (logging. WARNING)#Set File Level#2.1 Bind the handle object to the loggerLogger.addhandler (console) Logger.addhandler (text)#3. Generating Formatter ObjectsConsole_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') File_formatter= Logging. Formatter ('% (name) s-% (asctime) s-% (module) s-% (levelname) s-% (message) s') Console.setformatter (console_formatter) text.setformatter (file_formatter) logger.warning ("test1") Logger.info ("test2")

Python's common 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.