Python Basics-4.14 Logging module

Source: Internet
Author: User
Tags add time

Many programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into debug(), info(), warning(), error() and critical()5 levels, let's take a look at how to use

The simplest usage
Import Loggingin [5]: logging.warning ("user [Leco] attempted wrong password more than 3times" 
    3 timesin [6]: logging.critical ("serveris down"is down

Take a look at what these log levels mean, respectively.

Level When
it ' s used
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things is working as expected.
WARNING An indication this something unexpected happened, or indicative of some problem in the "near" (e.g. ' disk space low ') . The software is still working as expected.
ERROR Due to a more serious problem, the software have not been able to perform some function.
CRITICAL A serious error, indicating the program itself is unable to continue running.
If you want to write the log in a file, it's easy.
Import logginglogging.basicconfig (FileName='example.log', level=logging.info) Logging.debug ('Thismessage should go to the log file') logging.info (' So shouldthis') logging.warning ('and this, too' )

One of the level=loggin in the following sentence. Info means that the logging level is set to info, that is, only logs that are higher than the log is info or the info level will be recorded in the file, in this case, 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)
Custom log Format

Feel the above log format forgot to add time, log do not know how to line, the following to add!

Import logginglogging.basicconfig (Format='% (asctime) s% (message) s', datefmt='  %m/%d/%y%i:%m:%s%p') logging.warning ("is"when "thisevent was logged. ' )# output  is if this event was logged.

In addition to adding time, you can customize a large stack of formats, and the following table is all supported formats

% (name) s logger name
% (Levelno) s Log level in digital form
% (LevelName) s Log level in text form
% (pathname) s The full path name 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 Call the module name of the log output function
% (FuncName) s Function name of the call log output function
% (Lineno) d The line of code where the statement that called the log output function is located
% (created) f Current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d The number of milliseconds since logger was created when the log information is output
% (Asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d The thread ID. Probably not.
% (ThreadName) s The name of the thread. Probably not.
% (process) d The process ID. Probably not.
% (message) s User-Output messages
Log output to screen and file at the same time

If you want to print log in the screen and file log at the same time, you need to know a little bit more complex knowledge.

Python uses the logging module to log logs involving four main classes, which are best used in the official documentation:

    • Logger provides an interface that the application can use directly;
    • Handler sends the log record (created by logger) to the appropriate destination output;
    • Filter provides a fine-grained device to determine which log record to output;
    • Formatter determines the final output format of the log record.
The relationship between them is such that the main function of each component logger

Each program obtains a logger before outputting the information. Logger usually corresponds to the program's module name, such as the chat tool's graphical interface module can get its logger:

LOG=logging.getLogger(”chat.gui”)

And the core module can do this:

LOG=logging.getLogger(”chat.kernel”)

You can also bind handler and filters

Logger.setLevel(lel):指定最低的日志级别,低于lel的级别将被忽略。debug是最低的内置级别,critical为最高Logger.addFilter(filt)、Logger.removeFilter(filt):添加或删除指定的filterLogger.addHandler(hdlr)、Logger.removeHandler(hdlr):增加或删除指定的handler

Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical (): Log levels you can set

Handler

The handler object is responsible for sending relevant information to the specified destination. Python's log system can be used in a variety of handler. Some handler can output information to the console, some handler can output the information to a file, and some handler can send information to the network. If you feel that it is not enough, you can write your own handler. Multiple handler can be added through the AddHandler () method

Handler.setLevel(lel):指定被处理的信息级别,低于lel级别的信息将被忽略Handler.setFormatter():给这个handler选择一个格式Handler.addFilter(filt)、Handler.removeFilter(filt):新增或删除一个filter对象

Multiple handler can be attached to each logger. Next, let's introduce some common handler:

  1. Logging. Streamhandler uses this handler to output information to any file object, such as Sys.stdout or Sys.stderr.
  2. Logging. Filehandler and Streamhandler are similar for outputting log information to a file. But Filehandler will open this file for you.
  3. Logging.handlers.RotatingFileHandler

    This handler is similar to the filehandler above, but it can manage file sizes. 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. For example, the log file is Chat.log. When the chat.log reaches the specified size, Rotatingfilehandler automatically renames the file to Chat.log.1. However, if chat.log.1 already exists, it will first rename chat.log.1 to chat.log.2 ... Finally, re-create the Chat.log and continue to output the log information. Its functions are:

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

    Where the filename and mode two parameters are the same as the Filehandler. The

    • maxbytes is used to specify 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
    • backupcount is used to specify the number of reserved backup files. For example, if you specify 2, when the renaming process described above occurs, the original chat.log.2 is not renamed, but is deleted.
  4. Logging.handlers.TimedRotatingFileHandler

    This handler is similar to Rotatingfilehandler, however, it does not determine when to recreate the log file by judging the size of the file, but instead automatically creates a new log file 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:

    TimedRotatingFileHandler( filename [,when [,interval [,backupCount]]])

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

    intervalis the time interval.

    whenparameter is a string. A unit that represents a time interval, not case-sensitive. It has the following values:

    • s S
    • M min
    • H hours
    • D Day
    • W per week (Interval==0 on behalf of Monday)
    • Midnight every morning
Formatter components

The formatter of the log is a separate component that can be combined with handler

fh = logging.FileHandler("access.log")formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)fh.setFormatter(formatter) #把formmater绑定到fh上
Filter component

If you want to filter the log content, you can customize a filter

class ignorebackuplogfilter< Span class= "token punctuation" (Logging:  "" "Ignore log with DB Backup" "" def filter (Self:  #固定写法 return " DB Backup "not in record (      

Note that the filter function returns TRUE or False,logger determines whether this log is output based on this value

Then add this filter to the logger

logger.addFilter(IgnoreBackupLogFilter())

The following log will filter out filters that match the filter condition

logger.debug("test ....")logger.info("test info ....")logger.warning("start to run db backup job ....")logger.error("test error ....")

A complete example of output to screen, file, with filter at the same time

Import loggingClassIgnorebackuplogfilter(Logging. Filter):"" Ignores logs with DB backup ""DefFilter(Self, record):#固定写法Return"DB Backup"NotIn record. getMessage()#console Handlerch= Logging. Streamhandler() CH. setLevel(Logging. INFO)#file HANDLERFH= Logging. Filehandler(' Mysql.log ')#fh. SetLevel (logging. WARNING)#formatterformatter= Logging. Formatter('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')#bind Formatter to ChCh. setformatter(Formatter) FH. setformatter(Formatter) Logger= Logging. GetLogger("Mysql") Logger. setLevel(Logging. DEBUG)#logger priority is higher than other output paths#add handler to Logger Instancelogger. AddHandler(Ch) Logger. AddHandler(FH)#add Filterlogger. addfilter(Ignorebackuplogfilter()) logger. Debug("Test ...") logger. Info ("Test info ...") logger. Warning("start to run DB Backup job ...") logger. Error("Test error ....")            

Automatic file Truncation Example

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="S",interval=5,backupCount=3)formatter = logging.Formatter(‘%(asctime)s %(module)s:%(lineno)d %(message)s‘)fh.setFormatter(formatter)logger.addHandler(fh)logger.warning("test1")logger.warning("test12")logger.warning("test13")logger.warning("test14")

Python Basics-4.14 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.