python logging library

Author: yifei / Created: May 30, 2017, 11:54 a.m. / Modified: May 30, 2017, 11:55 a.m. / Edit

Yifei's Notes

Very sad, logging only supports %-style string formatting

logging.exception is the right way to log exceptions, not logging.error

Basic Usage

库只应该定义自己的日志的格式, 而不应该定义自己的日志如何输出, 日志输出应该由最终的使用程序来定义.   如果使用systemd来运行程序的话, 直接把所有日志打印到stdout就可以了

logging.basicConfig(level=level, format=fmt_string, filename=file, datefmt=datefmt)

if you don't supply filename, the log is printed to stdout

logging.debug('%string', args) # send to format string as message
logging messages

logging.getLogger.level = logging.DEBUG
# reset logging level
# basicConfig can only be called once in the main thread

python 的 logging


import logging
import mylib

def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)'Started')

if __name__ == '__main__':
import logging

def do_something():'Doing something')

If you run, you should see this in myapp.log:

INFO:root:Doing something


logging 模块提供了四种不同的模块

logger 是一棵树,默认的logger 是 root logger,logging 模块的方法也都是在调用这个logger

Logging in a library

get Logger

by defualt, the logger is called root logger.

You should use the factory method logging.getLogger(name) to instaniate a logger object, the name is supposed to be, so the recommnend value is __name__,That’s because in a module, __name__ is the module’s name in the Python package namespace.


logger = logging.getLogger(__name__) logger.addHandler(logging.NullHandler())



logging.basicConfig() # seems like logging.start... must be called to enable logging

turn off 3rd party logs


import logging

log = logging.getLogger(__name__)

log.setLevel(logging.INFO) # 设置logger的级别

handler = logging.NullHandler() handler.setLevel(logging.INFO) # 设置handler的级别

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter)



Logging serves two purposes: [1] • Diagnostic logging records events related to the application’s operation. If a user calls in to report an error, for example, the logs can be searched for context. • Audit logging records events for business analysis. A user’s transactions can be extracted and combined with other user details for reports or to optimize a business goal.


Here is an example of best practice from the requests source – place this in your import logging logging.getLogger(__name__).addHandler(logging.NullHandler())

日志应该看作是事件流, 不要自己管理日志, 把日志输出到stdout[2]


logging模块是thread safe的, 至少理论上来说是的...

有任何问题可以发邮件到 kongyifei (at) 讨论