如何设置python日志记录模块将异常写入文件处理程序?在控制台中我自然会看到系统异常,但是当我没有用try块明确地将它包围时,如何配置日志记录以将其发送到文件处理程序?
import logging
import logging.config
logging.config.fileConfig('logging.conf')
log = logging.getLogger('my_file_logger')
log.info('writes to logfile')
print('prints to console')
not_a_command # force exception
和logging.conf
[loggers]
keys=root,console,my_file_logger
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=simpleFormatter
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
[handler_consoleHandler]
class=StreamHandler
formatter=simpleFormatter
level=DEBUG
args=(sys.stdout,)
stream=sys.stdout
[handler_fileHandler]
class=FileHandler
formatter=simpleFormatter
level=DEBUG
args=('logfile_'+time.strftime('%%Y%%m%%d', time.localtime())+'.log', 'a')
[logger_root]
level=ERROR
handlers=consoleHandler
[logger_console]
level=DEBUG
propagate=0
qualname=console
handlers=consoleHandler
[logger_my_file_logger]
level=DEBUG
propagate=0
qualname=my_file_logger
handlers=consoleHandler,fileHandler
谢谢!