日志管理 logging

日志级别,默认是waring

DEBUG 详细信息

INFO 正常运行产生的一些信息

WARNING 警告用户,虽然正常但可能会发生错误

ERROR 严重错误

CRITICAL 严重错误

简单应用

基础配置

python 复制代码
#filename,日志输出的文件,filemode='w',每次运行后会清除上次的日志内容,level=logging.DEBUG,表示只要在这个等级(包括)上的都会被输出
logging.basicConfig(filename='demo.log',filemode='w',level=logging.DEBUG)
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")

输出变量

python 复制代码
logging.basicConfig(level=logging.DEBUG)
logging.debug("姓名 %s,年龄 %d","张三",19)
logging.debug("姓名 %s,年龄 %d"%("张三",19))

设置输出格式

高级应用

loggers:记录器,提供应用程序代码直接使用的接口

Handlers:处理器,将日志输出到指定的位置

Filters:过滤器,更好选择输出的日志

Formatters:格式化器

python 复制代码
#记录器
logger = logging.getLogger("app_log")
logger.setLevel(logging.DEBUG)

#处理器
#输出控制台
consoleHandle = logging.StreamHandler()
consoleHandle.setLevel(logging.DEBUG)

#输出文件
fileHandle = logging.FileHandler(filename="addDemo.log")
fileHandle.setLevel(logging.INFO)

#formatter格式
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

#设置处理的的输出格式
consoleHandle.setFormatter(formatter)
fileHandle.setFormatter(formatter)

#记录器要设置处理器
logger.addHandler(consoleHandle)
logger.addHandler(fileHandle)

#过滤器
flt = logging.Filter("cc")

#不会输出日志,被过滤掉
# logger.addFilter(flt)
#指定过滤对应的处理器
fileHandle.addFilter(flt)

logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
logger.critical("critical message")

使用配置文件

配置文件

python 复制代码
[loggers]
keys=root,app_log

[handlers]
keys=fileHandler,consoleHandle

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandle

[logger_app_log]
level=DEBUG
handlers=fileHandler,consoleHandle
qualname=app_log
propagate=0

[handler_consoleHandle]
class=StreamHandler
args=(sys.stdout,)
level=DEBUG

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

[formatter_simpleFormatter]
format=%(asctime)s|%(levelname)8s|%(filename)s[:%(lineno)d]|%(message)s
datefmt=%Y-%m-%d %H:%M:%S
python 复制代码
#配置文件方式处理日志
logging.config.fileConfig('logging.conf')
#记录器
rootLogger = logging.getLogger()
logger = logging.getLogger("app_log.log")

a="sss"
try:
    int(a)
except Exception as e:
    logger.exception(e)


logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
logger.critical("critical message")
相关推荐
夜猫逐梦30 分钟前
【逆向经验】一篇文章讲透为什么CE搜不到Python游戏的内存值
开发语言·python·游戏
超梦dasgg1 小时前
智慧充电系统设备管理服务对外接口实现方案
java·spring·微服务
水龙吟啸1 小时前
数据结构与算法随机复习–Day1
数据结构·c++·算法
SilentSamsara1 小时前
闭包的本质:Python 如何捕获自由变量
开发语言·python·青少年编程·pycharm
十五年专注C++开发1 小时前
浅谈LLVM
开发语言·c++·qt·clang·llvm
xiaoye37081 小时前
Spring 事务传播机制 + 隔离级别
java·后端·spring
白夜11172 小时前
C++(标签派发 Tag Dispatching)
开发语言·c++·笔记·算法
王老师青少年编程2 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串基础】:凯撒密码
c++·字符串·csp·凯撒密码·高频考点·信奥赛·一等奖
Arya_aa2 小时前
数据字典模块–JSR303参数校验
java
CSCN新手听安2 小时前
【Qt】Qt窗口(六)QMessageBox消息对话框的使用
开发语言·c++·qt