logging module (c) - logging logging framework

(A) a frame part of the logging logs

  • Logger: That Logger Main Class, the object is created when logging in us, we can call its methods of incoming information and log template to generate a log record, called the Log Record.
  • Handler: i.e., classes for handling logging, which can be output to the Log Record we specify the location and the log storage format, etc., as we can specify the log to a remote server via the FTP protocol, Handler will help us to complete These things.
  • Formatter: Log Record is actually generated one object, then we want to save them to a section of the log text we want, then you need to have a formatting process, then the process would be completed by the Formatter, return log string is then passed back to the Handler to handle.
  • Filter: In addition to save the log may not need all the time we save, we may only need to save part of what we want on it, so before saving it also needs to be filtered, leaving a log we want, if only to save a level logging, or just save a log of a keyword, etc., then the filtering process on to filter to complete.

(Ii) written to the log file main processes:

  1. Creating a logger objects
  2. Set the log level level
  3. Create a handler, a log file for writing
  4. Setting the output format handler formatter
  5. The logger after the filter filtration send handler

(C) Example:

Use the logging module logs log file

'''
logging日志框架-logger,formatter,filter,handler
'''

import logging

#创建一个logger对象
logger = logging.getLogger()

#设置logger日志级别
logger.setLevel(logging.NOTSET)

#设置日志去向
handler=logging.FileHandler(filename='study02.log', encoding='utf-8',)

#设置实际输入到日志文件的日志级别
handler.setLevel(logging.ERROR)

#设置日志格式
formatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

#给logger对象添加日志去向
logger.addHandler(handler)

logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')
logger.error('this is error message')
logger.critical('this is cirtical message')




 

 

(D) Extended

Loggers:

Logger objects do three things.

First, they are exposed to a number of methods application code, so that the application message can be recorded at runtime.

Second, the object to determine which records a log message needs to be recorded by severity (default filter facilities) or filter objects.

Third, the object record log messages related transfer to all interested log handler.

Conventional method of recording object into two categories: Configuration and send messages.

These are the most common configuration methods:

Logger.setLevel () the lowest level of security log information specified logger will be processed.

Logger.addHandler () to add the recorder handler object from the object.

Logger.addFilter () was added from a filter object record object.

Handlers

处理程序The object is responsible for the appropriate message log (log messages based on the severity of) assigned to the specified destination handler.

Logger Objects can be addHandler()increased zero or more objects handler method. For example, an application can send all log messages to the log file, all error level (error) and above the log messages sent to standard output, all severity levels (critical) log messages to an electronic mailbox. In this example, it requires three separate processors, each responsible for sending a specific message to a specific level position.

There are four commonly used: 

1) logging.StreamHandler -> console output 


Handler can use this information to any output file object is similar to sys.stdout or sys.stderr of (file object).

It is constructor: StreamHandler ([strm])

  • Strm parameter which is a file object. The default is sys.stderr


2)   logging.FileHandler -> File Output


Similarly StreamHandler and for outputting information to a log file. But FileHandler will help you open the file.

It is constructor: FileHandler (filename [, mode])

  • filename is the file name, you must specify a file name.
  • mode is to open the way to a file. The default is 'a', is added to the end of the file.


. 3)   logging.handlers.RotatingFileHandler -> log file according to the size of the automatic segmentation, once the specified file size is reached rebuild 


The Handler similar to the above FileHandler, but it can manage file size. When the file reaches a certain size, it will automatically rename the current log file, and then create a new log file of the same name continues to be output. For example, the log file is chat.log. When chat.log reaches the size specified, RotatingFileHandler files automatically renamed chat.log.1. However, if chat.log.1 already exists, will first chat.log.1 rename chat.log.2. . . Finally, re-create chat.log, it continues to output log information.

It is constructor: RotatingFileHandler (filename [, mode [, maxBytes [, backupCount]]])

  • Where filename and mode FileHandler two parameters and the same.
  • MaxBytes file is used to specify the maximum log file size. If maxBytes is zero, this means that log files can be infinite, then renaming process described above does not occur.
  • backupCount for the specified number of backup files to keep. For example, if you specify 2, when the renaming process described above occurs, the original chat.log.2 will not be renamed, but was deleted.


. 4)   logging.handlers.TimedRotatingFileHandler ->  Automatic log file segmentation in time 


The Handler and RotatingFileHandler similar, but it does not have to decide when to re-create the log file to determine the file size, but a certain time interval automatically create a new log file. Renaming process and RotatingFileHandler similar, but the new files are not figures, but the current time.

It is constructor: TimedRotatingFileHandler (filename [, when [, interval [, backupCount]]])

  • Where filename parameter and backupCount parameters and RotatingFileHandler have the same meaning.
  • interval is the time interval.
  • when parameter is a string. Expressed in units of time intervals, a case-insensitive. It has the following values:
  • S seconds
  • M points
  • H-hour
  • D-day
  • W per week (interval == 0 behalf Monday)
  • midnight every morning

 


Reference Address: https://www.cnblogs.com/nancyzhu/p/8551506.html

gitbuh Address:  https://github.com/panc-test/python-study.git

 

Published 46 original articles · won praise 39 · views 20000 +

Guess you like

Origin blog.csdn.net/panc_guizaijianchi/article/details/104339874
Recommended