日志管理 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")
相关推荐
宠友信息14 分钟前
内容社区源码开发实践解析,用Spring Boot打造1:1仿小红书源码平台
java·spring boot·redis·websocket·mysql·spring·uni-app
qq_5896660518 分钟前
C语言、C++与C#的区别详解
c语言·c++·c#
行百里er27 分钟前
加个依赖就生效?一行搞定 Spring Boot Starter 自动装配
java·后端·监控
月华路34 分钟前
G1 新生代对象晋升老年代:实现机制与 GC 日志
java·jvm·算法
万法若空41 分钟前
排列组合恒等式
c++·算法
Data_Journal43 分钟前
如何使用 Python 抓取 Google Flights:分步指南
大数据·开发语言·数据库·python·scrapy
AI人工智能+电脑小能手1 小时前
大白话说Java设计模式-34-命令模式(业务实战篇)
java·spring·设计模式·命令模式·异步任务·撤销重做·事务封装
净重21克1 小时前
万字长文!TLS协议升级避坑手册:JDK老项目适配TLS1.2/1.3全流程拆解
java·springboot·java安全
一直C1 小时前
【数据结构】哈希表+算法复杂度与经典排序查找(C语言)
java·linux·开发语言·数据结构·算法·ubuntu·散列表
paopaokaka_luck1 小时前
基于springboot3+vue3的车间生产管理系统(Echarts图形化分析、BI报表)
java·前端·spring boot·学习·echarts