按照官方文档的介绍,logging 是线程安全的,也就是说,在一个进程内的多个线程同时往同一个文件写日志是安全的。但是(对,这里有个但是)多个进程往同一个文件写日志不是安全的。官方的说法是这样的:
Because there is no standard way to serialize access to a single file across multiple processes in Python. If you need to log to a single file from multiple processes,
one way of doing this is to have all the processes log to a SocketHandler, and have a separate process which implements a socket server which reads from the socket and logs to file.
(If you prefer, you can dedicate one thread in one of the existing processes to perform this function.)
有的人会说,那我不用多进程不就可以了。但是 Python 有一个 GIL 的大锁(关于 GIL 的纠葛可以看这里),使用多线程是没法利用到多核 CPU 的,大部分情况下会改用多进程来利用多核 CPU,因此我们还是绕不开不开多进程下日志的问题。
为了解决这个问题,可以使用 ConcurrentLogHandler,ConcurrentLogHandler 可以在多进程环境下安全的将日志写入到同一个文件,并且可以在日志文件达到特定大小时,分割日志文件。在默认的 logging 模块中,有个 TimedRotatingFileHandler 类,可以按时间分割日志文件,可惜 ConcurrentLogHandler 不支持这种按时间分割日志文件的方式。
重新修改下 handlers 中的 class。
logging.config.dictConfig({
...
'handlers'
: {
'file'
: {
'level'
:
'DEBUG'
,
# 如果没有使用并发的日志处理类,在多实例的情况下日志会出现缺失
'class'
:
'cloghandler.ConcurrentRotatingFileHandler'
,
# 当达到10MB时分割日志
'maxBytes'
:
1024
*
1024
*
10
,
# 最多保留50份文件
'backupCount'
:
50
,
# If delay is true,
# then file opening is deferred until the first call to emit().
'delay'
:
True
,
'filename'
:
'logs/mysite.log'
,
'formatter'
:
'verbose'
}
},
...
})
|
1.以配置文件的方式使用logging模块
说明:使用logging.config.fileConfig("Logging.ini")载入后,启动程序报错.KeyError Formatters 在网上查了很久,说是日志路径的问题,结果就是路径问题
[loggers]
keys=root,infoLogger,errorlogger
[logger_root]
level=DEBUG
handlers=infohandler,errorhandler
[logger_infoLogger]
handlers=infohandler
qualname=infoLogger
propagate=0
[logger_errorlogger]
handlers=errorhandler
qualname=errorlogger
propagate=0
###############################################
[handlers]
keys=infohandler,errorhandler
[handler_infohandler]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stdout,)
[handler_errorhandler]
class=FileHandler
level=ERROR
formatter=form01
args=('logs/mylog.log', 'a')
###############################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_form02]
format=%(asctime)s %(filename)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
字段说明:
[loggers]
# 定义logger模块,root是父类,必需存在的,其它的是自定义。
# logging.getLogger(NAME)便相当于向logging模块注册了一种日志打印
# name 中用 . 表示 log 的继承关系
[handlers]
# 定义handler
[formatters]
# 定义格式化输出
[logger_root]
# 实现上面定义的logger模块,必需是[logger_xxxx]这样的形式
# [logger_xxxx] logger_模块名称
# level 级别,级别有DEBUG、INFO、WARNING、ERROR、CRITICAL
# handlers 处理类,可以有多个,用逗号分开
# qualname logger名称,应用程序通过 logging.getLogger获取。对于不能获取的名称,则记录到root模块。
# propagate 是否继承父类的log信息,0:否 1:是
[handler_infohandler]
# [handler_xxxx]
# class handler类名
# level 日志级别
# formatter,上面定义的formatter
# args handler初始化函数参数
[formatter_form01]
# 日志格式
#--------------------------------------------------
|
使用方式:
from
logging.config
import
fileConfig
fileConfig(
'loggin_config.ini'
)
logger
=
logging.getLogger(
'infoLogger'
)
logger.info(
'test1'
)
logger_error
=
logging.getLogger(
'errorhandler'
)
logger_error.error(
'test5'
)
|
2.在code中直接使用logging模块