Python logging module

Source: Internet
Author: User
Tags add time

Logging module

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() and error() critical() 5个级别, below we look at how to use.

The simplest usage

12345678 importlogging logging.warning("user [alex] attempted wrong password more than 3 times")logging.critical("server is down") #输出WARNING:root:user [alex] attempted wrong password more than 3 timesCRITICAL:root:server isdown

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.

123456 import  logging  logging.basicconfig (filename = ' Example.log ' ,level = logging.info) logging.debug ( "This message should go to the log file ' ) logging.info ( ' so should this ' 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.

1 logging.basicConfig(filename=‘example.log‘,level=logging.INFO)

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

123456 importlogginglogging.basicConfig(format=‘%(asctime)s %(message)s‘, datefmt=‘%m/%d/%Y %I:%M:%S %p‘)logging.warning(‘is when this event was logged.‘)#输出12/12/201011:46:36AM iswhen this event was logged.

Log format

% (name) s

Logger's 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

  

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.

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")

Logger.setlevel (LEL): Specifies the lowest log level, and the level below LEL is ignored. Debug is the lowest built-in level, critical is the highest
Logger.addfilter (filt), Logger.removefilter (filt): Add or remove the specified filter
Logger.addhandler (HDLR), Logger.removehandler (HDLR): Add or remove the specified 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 logger 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): Specifies the level of information being processed, and information below the LEL level is ignored
Handler.setformatter (): Choose a format for this Handler
Handler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter object


Multiple handler can be attached to each logger. Next, let's introduce some common handler:
1) logging. Streamhandler
Using this handler, you can output information to any file object, such as Sys.stdout or Sys.stderr. Its constructor is:
Streamhandler ([STRM])
Where the STRM parameter is a file object. Default is Sys.stderr


2) logging. Filehandler
Similar to Streamhandler, used to output log information to a file. But Filehandler will open this file for you. Its constructor is:
Filehandler (Filename[,mode])
FileName is a file name and you must specify a file name.
Mode is how the file is opened. See the use of the Python built-in function open (). The default is ' a ', which is added to the end of the file.

3) Logging.handlers.RotatingFileHandler
This handler is similar to the filehandler above, but it can manage the file size. 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 constructor is:
Rotatingfilehandler (filename[, mode[, maxbytes[, Backupcount]])
where filename and mode two parameters are the same as 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 constructor is:
Timedrotatingfilehandler (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, 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

12345678910111213141516171819202122232425262728293031 importlogging#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(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)# add formatter to ch 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‘)

  

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 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.