Python Logging Module
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:
attributes | discription |
---|---|
%(name)s | Name of the logger (logging channel) |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
%(levelname)s | Text logging level for the message (“DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”) |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available) |
%(filename)s | Filename portion of pathname |
%(module)s | Module (name portion of filename) |
%(lineno)d | Source line number where the logging call was issued (if available) |
%(funcName)s | Function name |
%(created)f | Time when the LogRecord was created (time.time() return value) |
%(asctime)s | Textual time when the LogRecord was created |
%(msecs)d | Millisecond portion of the creation time |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time) |
%(thread)d | Thread ID (if available) |
%(threadName)s | Thread name (if available) |
%(process)d | Process ID (if available) |
%(message)s | The 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