Python's logging, recording the packet of log

Source: Internet
Author: User

recently when doing automated testing, want to add a log to him, so use the logging module, the following is the Python add log several waysFirst, the Python code configuration (of course, there is a multi-module common Python code settings, this online there are many examples, it is not here to repeat)

Import logging
Import Logging.handlers

#LOG_FILE is the file address of the log to output
log_file=r "C:\Users\min.sun\Desktop\ Automation test \log.txt"
handler = Logging.handlers.RotatingFileHandler (log_file, MaxBytes = 1024*1024, Backupcount = 5)
FMT = "% (asctime) s-% (filename) s:% (lineno) s-% (name) s-% (message) s"
formatter = logging. Formatter (FMT)
Handler.setformatter (Formatter)
logger = Logging.getlogger (' TST ')
Logger.addhandler (handler)
Logger.setlevel (logging. DEBUG)
Logger.info (' first info message ')
logger.debug (' first debug message ')
Try:
1/0
except Exception as E:
Logger.debug (e)
print (e) Note: The log can contain variables, as follows, to replace the contents of the name variable with%s. Not only info, but also other levels.Logger.info ("This is a variable%s", name)
 Second, use the configuration fileThe use of configuration files can be more flexible, more convenient, the following are some of the configuration file usage methods configuration file:
[Loggers]
keys=root,example01,example02,performtest

[Logger_root]
Level=debug
handlers=hand01,hand02

[LOGGER_EXAMPLE01]
handlers=hand03
qualname=example01
propagate=0

[LOGGER_EXAMPLE02]
handlers=hand01,hand03
QUALNAME=EXAMPLE02
propagate=0

[Logger_performtest]
handlers=hand03
qualname=performtest
propagate=0
###############################################
[formatters]
keys=form01,form02

[FORMATTER_FORM01]
format=% (asctime) s-[% (filename) s:% (Lineno) s]-% (levelname) s-% (message) s
datefmt=

[FORMATTER_FORM02]
format=% (name) -12s:% (levelname) -8s% (message) s
datefmt=%a,%d%b%Y%h:%m:%s

###############################################

[Handlers]
keys=hand01,hand02,hand03

[HANDLER_HAND01]
Class=streamhandler
Level=info
formatter=form02
args= (Sys.stderr,)

[HANDLER_HAND02]
Class=filehandler
Level=debug
formatter=form01
#要存储log的文件地址
Args= (R "C:\Users\min.sun\Desktop\testlog.txt", ' a ')

[HANDLER_HAND03]
Class=handlers. Rotatingfilehandler
Level=info
Formatter=form01
Args= (R "C:\Users\min.sun\Desktop\testlog.txt", ' a ', 10*1024*1024, 5)

(Note: Focus on the handler in the class,logging provides a variety of handler, different representative of the different log storage, the specific content can see my reference)
Methods that are called in the module:
Import logging
Import Logging.config
Logging.config.fileConfig (r "C:\Users\min.sun\Desktop\autotest\com\log.conf")
Logger = Logging.getlogger ("example01")

Logger.debug ("Debug Message")
Logger.info ("info message")
Logger.warn ("Warn message")
Logger.error ("error message")
Logger.critical ("Critical Message"
Note: In the process of using logging encountered the problem of encoding, one is the configuration file stored in the log file path contains Chinese, but the execution prompt gbk can not read; one is the stored log contains Chinese, the results are stored in the log file after garbled.
Workaround: Encode the log file change to UTF8 no BOM format encoding. The configuration file is also changed to this format (but sometimes not, so this needs to be further understood)


Reference article: http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

Python's logging, recording the packet of log

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.