1. 程式人生 > >python logging 的配置檔案

python logging 的配置檔案

2012-03-27

http://abloz.com author:ablozhou date:2012.3.27

python 自帶logging模組,功能和log4cpp,log4j差不多。可以用logging.conf來寫log需要輸出的格式和相關配置。 下面是我的一個配置檔案。其中的TimedRotatingFileHandler和RotatingFileHandler配置方式可以參考,可以完成按時間段或時間點或按檔案大小切換log檔案。 另外,如果自定義時間格式,比如要精確到毫秒,如何配置呢?時間的引數和time.strftime()一樣。但格式化引數裡面只有秒,沒有毫秒,毫秒數如何顯示呢?下面也有示例。還有就是輸出格式上,增加空格以對齊,都可以參考。


#http://abloz.com
#author:ablozhou
#date:2012.3.27

[loggers]
keys=root,simple

[handlers]
keys=consoleHandler,TimedRotatingFileHandler,RotatingFileHandler,FileHandler

[formatters]
keys=simpleFormatter,timedRotatingFormatter

[logger_root]
level=DEBUG

#Level	Numeric value
#CRITICAL	50
#ERROR	40
#WARNING	30
#INFO	20
#DEBUG	10
#NOTSET	0

handlers=consoleHandler,TimedRotatingFileHandler
#FileHandler

[logger_simple]
level=DEBUG
handlers=consoleHandler
qualname=simple
propagate=0

[formatter_timedRotatingFormatter]
format=%(asctime)s.%(msecs)d %(name)-12s %(levelname)-8s %(message)s
datefmt=%y-%m-%d %H:%M:%S
#datefmt=%y-%m-%d %H:%M:%S %p

[handler_RotatingFileHandler]
class=handlers.RotatingFileHandler
level=INFO
formatter=simpleFormatter
args=('./logs/perfrf.log', 'a', '10240', 10)
#(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)

[handler_TimedRotatingFileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=timedRotatingFormatter
args=('./logs/perft.log', 'M', 10, 5)
#(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)

#Value	Type of interval
#'S'	Seconds
#'M'	Minutes
#'H'	Hours
#'D'	Days
#'W'	Week day (0=Monday)
#'midnight'	Roll over at midnight

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_FileHandler]
class=TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=("perf.log",)

[handler_FileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=("./logs/perf.log",)


[formatter_simpleFormatter]
format=%(asctime)s [%(levelname)s][%(name)s]:%(message)s   (%(filename)s:%(lineno)d)
#format=%(asctime)s[%(levelname)s][%(name)s]:%(message)s
datefmt=

參考 配置: http://docs.python.org/howto/logging.html#configuring-logging 時間格式: http://docs.python.org/library/time.html#time.strftime

如非註明轉載, 均為原創. 本站遵循知識共享CC協議,轉載請註明來源