我正在尝试使用 python 日志记录模块RotatingFileHandler
为我的程序创建一个。我的日志处理程序将输出记录到文件中:/var/log/pdmd.log
并且基本功能似乎可以正常工作并根据需要记录输出。
但是,我正在尝试使用以下格式格式化我的日志字符串:
"%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s"
但只有message
部分异常被记录下来。这是我设置记录器的代码:
#class variable declared at the beginning of the class declaration
log = logging.getLogger("PdmImportDaemon")
def logSetup(self):
FORMAT = "%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s"
logging.basicConfig(format=FORMAT)
#logging.basicConfig(level=logging.DEBUG)
self.log.setLevel(logging.DEBUG) #by setting our logger to the DEBUG level (lowest level) we will include all other levels by default
#setup the rotating file handler to automatically increment the log file name when the max size is reached
self.log.addHandler( logging.handlers.RotatingFileHandler('/var/log/pdmd.log', mode='a', maxBytes=50000, backupCount=5) )
现在,当我运行一个方法并使用以下代码将程序输出到日志时:
def dirIterate( self ):
try:
raise Exception( "this is my exception, trying some cool output stuff here!")
except Exception, e:
self.log.error( e )
raise e
文件中的输出pdmd.log
只是异常文本,没有别的。由于某种原因,格式没有得到尊重;我期望:
ERROR 2013-09-03 06:53:18,416 dirIterate 89 this is my exception, trying some cool output stuff here!
关于为什么我设置的格式logging.basicConfig
没有得到尊重的任何想法?