您的位置:首页 > 编程语言 > Python开发

Python Logging Module

2017-11-20 14:18 369 查看

Main stuff

logger, handler, formatter.

How it works



ref:

How Python logging module works | Shut Up and Ship

http://zqpythonic.qiniucdn.com/data/20161124153540/index.html

Simple example

import logging
# create logger
lgr = logging.getLogger('myapp')
lgr.setLevel(logging.DEBUG)
# add a file handler
fh = logging.FileHandler('myapp.log')
fh.setLevel(logging.WARNING)
# create a formatter and set the formatter for the handler.
frmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(frmt)
# add the Handler to the logger
lgr.addHandler(fh)
# You can now start issuing logging statements in your code
lgr.debug('debug message') # This won't print to myapp.log
lgr.info('info message') # Neither will this.
lgr.warning('Checkout this warning.') # This will show up in the log file.
lgr.error('An error goes here.') # and so will this.
lgr.critical('Something critical happened.') # and this one too.


Levels

CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

ref:

How Python logging module works | Shut Up and Ship

http://zqpythonic.qiniucdn.com/data/20161124153540/index.html

More to understand the work flow



formatter

Currently, the useful attributes in a LogRecord are described by:

attributesdiscription
%(name)sName of the logger (logging channel)
%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)
%(levelname)sText logging level for the message (“DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”)
%(pathname)sFull pathname of the source file where the logging call was issued (if available)
%(filename)sFilename portion of pathname
%(module)sModule (name portion of filename)
%(lineno)dSource line number where the logging call was issued (if available)
%(funcName)sFunction name
%(created)fTime when the LogRecord was created (time.time() return value)
%(asctime)sTextual time when the LogRecord was created
%(msecs)dMillisecond portion of the creation time
%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time)
%(thread)dThread ID (if available)
%(threadName)sThread name (if available)
%(process)dProcess ID (if available)
%(message)sThe result of record.getMessage(), computed just as the record is emitted
ref:

cpython: 5c4ca109af1c Lib/logging/__init__.py

https://hg.python.org/cpython/file/5c4ca109af1c/Lib/logging/__init__.py#l399

logging.conf

python 的日志logging模块学习 - dkcndk - 博客园

https://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

16.7. logging.config — Logging configuration — Python 3.6.3 documentation

https://docs.python.org/3/library/logging.config.html

reference

Python Standard Logging in Sugar - OLPC

http://wiki.laptop.org/go/Python_Standard_Logging_in_Sugar

How Python logging module works | Shut Up and Ship

http://zqpythonic.qiniucdn.com/data/20161124153540/index.html

Python 学习入门(14)—— logging - CSDN博客

http://blog.csdn.net/ithomer/article/details/16985379
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: