日志

1.配置文件(logging.conf)

[loggers]
keys=root,applog

[handlers]
keys=fileHandler,consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_applog]
level=DEBUG
handlers=fileHandler,consoleHandler
qualname=applog
propagate=0


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

[handler_fileHandler]
class=handlers.TimedRotatingFileHandler
args=("applog.log","midnight",1,0)
level=DEBUG
formatter = simpleFormatter

[formatter_simpleFormatter]
format=%(asctime)s|%(levelname)8s|%(name)s:%(lineno)s|%(message)s|%(processName)s
datafm=%Y-%m-%d %H:%M:%S

2.代码

import logging.config

#使用日志配置文件
logging.config.fileConfig('logging.conf')

rootLogger = logging.getLogger()
rootLogger.debug('This is root Logger,debug')

#记录器
logger = logging.getLogger('applog')#修改的显示名称
#打印日志的代码
logger.debug('This is debug log,bug')#调试界别

#设置报错将str转换成int


a = 'abc'
try:
    int(a)
except Exception as e:
    logger.exception(e)

3.效果(将会记录日志到applog.log里)

 

 

参阅:https://blog.csdn.net/yuan2019035055/article/details/123790394

 

posted @ 2022-12-02 09:39  蜗牛的礼物  阅读(24)  评论(0编辑  收藏  举报