Log processing in Python

Source: Internet
Author: User

In the daily project, always need to record some small information or error code, error message, this time need to do the log operation.
The modules used in Python for log creation, Setup, and logging are logging, and the following is an introduction to their basic usage:

First, the most simple way to use:


Import logging

Log_file = "/opt/xxx/log/debug.log"
Logging.basicconfig (filename=log_file,level=logging. DEBUG)
then refer to the place where you need to log:

Logging.warn ("[Warn:%s]warn messages%s", error_code,error_msg)
A
few simple lines of code can be implemented to write "[Warn:14001]warn message Error Request" to the log file/opt/xxx/log/debug.log.


Second, the more formal use of the way:


#!/usr/bin/python

Def initlog ():
Import logging

# Generate a Log Object
Logger = Logging.getlogger ()
# generates a handler. Logging supports many handler, such as Filehandler, Sockethandler, Smtphandler, etc.,
# I used Filehandler because I wanted to write a file.
# log_file is a global variable, it is a file name, such as: ' Crawl.log '
Log_file = "/opt/xxx/log/debug.log"
HDLR = logging. Filehandler (Log_file)
# generates a formatter that is used to standardize the output format of the log. Without this line of code, the default
# The format is: "% (message) s". That is, when the log is written, what is the information in the log?
# no date, no information level, etc. Logging supports a number of replacement values, see more
# Formatter Document description. Here are three items: time, information level, log information
Formatter = logging. Formatter ('% (asctime) s% (levelname) s% (message) s ')
# Set the formatter on the processor
Hdlr.setformatter (Formatter)
# Add the processor to the log object
Logger.addhandler (HDLR)
# Set the level of log information output. Logging provides various levels of log information, such as: NOTSET,
# DEBUG, INFO, WARNING, ERROR, critical, etc. Each level corresponds to a numeric value.
# If this sentence is not executed, the default is (WARNING). Can do: Logging.getlevelname
# (Logger.geteffectivelevel ()) to see the default log level. Log objects for different
# The level information provides different functions for output, such as: info (), error (), debug (), and so on. When
# information that is less than the specified level is ignored when writing to the log. So in order to output the desired log level must be
# to set this parameter. Here I set to NotSet (value 0), that is, I want to output all the information
Logger.setlevel (logging. NOTSET)
return Logger

logging = Initlog ()
Logging.info (' Registration ')
Logging.error (' error! ')
Logging.debug (' Debug ')
the usage in the above code allows you to specify the level and format of the log, which can be used to differentiate between the level of logs in the development environment and the user environment and reduce unnecessary waste of space.

Third, log segmentation

when the project is running for a period of time, it will find that the log file is getting bigger and larger, and it is not easy to find the error message for one day. This requires us to split the log, divided into two types: Rotatingfilehandler (split by file size), Timedrotatingfilehandler (split by time interval)
    • Based on the code in the second example, the use of file size segmentation is as follows:

HDLR =logging.handlers.rotatingfilehandler (log_file,maxbytes=1024*1024,backupcount=40)
where maxbytes specifies the size of each log file, the maximum number of backup files is 40 if the file exceeds 1024 bits to split the log file. To Log_file in the directory under the view, found in addition to debug.log files, but also more debug.log.1,debug.log.2 and other files.

debug.log files and debug.log.1, such as the relationship between the file is: Debug.log is the complete, debug.log.1 and debug.log.2 and other files are not the intersection of a subset of Debug.log (forget how to write the mathematical symbols, feel the words of wood expression is clear ah)
    • Similarly, based on the code in the second example, the use of split by time interval:
HDLR = Logging.handlers.TimedRotatingFileHandler (log_file,when= ' M ', interval=1,backupcount=40)
The When
parameter can be set to S-second (split the log in seconds), M-minutes (split the log by minute), H-hours (split the log by the Hour), D-days (split the log by day), Midnigh-roll over At midnight (logs are rolled back at midnight every day), w{0-6}-roll over a certain day;0-monday (rollback of logs by a specified date such as 0-Monday). When the parameter is "H" by the hour, the argument is not sensitive to case, so it doesn't matter if it's H or H.
Interval parameter Default "1", if when= ' H ', then is every hour to split the log, that is, Debug.log directory will appear debug.log.2013-06-28_13,debug.log.2013-06-28_ Log files such as 14.

Log processing in Python

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.