Python logging模块进行封装实现原理解析
作者:不放弃自己 时间:2021-02-15 07:51:44
1. 简介
追踪某些软件运行时所发生事件的方法, 可以在代码中调用日志中某些方法来记录发生的事情
一个事件可以用一个可包含可选变量数据的消息来描述
事件有自己的重要性等级
2. 使用logging日志系统四大组件
loggers日志器
提供应用程序代码直接使用的接口
handlers处理器
用于将日志记录发送到指定的目的位置
filters过滤器
过滤, 决定哪些输出哪些日志记录, 其余忽略
formatters格式器
控制日志输出格式
使用代码如下
import os, time, logging, sys
from Common.plugs.get_config import r_config
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
if sys.platform == "win32":
ENV_CONF_DIR = os.path.join(BASE_DIR, 'Common/conf/env_config.ini').replace('/', '\\')
else:
ENV_CONF_DIR = os.path.join(BASE_DIR, 'Common/conf/env_config.ini')
log_path = r_config(ENV_CONF_DIR, "log", "log_path")
class Log:
def __init__(self, log_path):
self.logName = os.path.join(log_path, '{0}.log'.format(time.strftime('%Y-%m-%d')))
def console_log(self, level, message):
# 创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于 debug 写入日志文件
debug_file = logging.FileHandler(self.logName, 'a+', encoding='utf-8')
debug_file.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
debug_file.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(debug_file)
logger.addHandler(ch)
# 记录一条日志
if level == 'info':
logger.info(message)
elif level == 'debug':
logger.debug(message)
elif level == 'warning':
logger.warning(message)
elif level == 'error':
logger.error(message)
elif level == 'critical':
logger.critical(message)
logger.removeHandler(ch)
logger.removeHandler(debug_file)
debug_file.close()
def debug(self, message): #最详细日志信息, 多用于问题诊断
self.console_log('debug', message)
def info(self, message): #仅次于DEBUG, 多用于记录关键点信息, 确保程序按预期执行
self.console_log('info', message)
def warning(self, message): #低等级故障, 但程序仍能运行, 如磁盘空间不足警告
self.console_log('warning', message)
def error(self, message): #由于比WARNING严重的问题, 导致某些功能不能正常运行时的记录
self.console_log('error', message)
def critical(self, message): 严重错误, 导致应用程序不能继续运行时的记录
self.console_log('critical', message)
if __name__ == '__main__':
Log(log_path).info("adasd")
Log(log_path).error("dsadasddasd")
'''
来源:https://www.cnblogs.com/hghua/p/13268007.html
标签:Python,logging,模块,封装
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp Driver和Provider两种连接字符串连接Access时的区别
2011-03-09 11:19:00
Transact_SQL小手册,适合初学者
2008-08-25 19:40:00
30个最常用css选择器解析
2011-06-16 20:36:37
开心网上input输入框研究
2009-03-06 12:52:00
![](https://img.aspxhome.com/file/UploadPic/20093/6/1207fc9f5fag215-89s.jpg)
用ASP在线创建Word与Excel文档
2008-07-20 19:17:00
Django框架实现分页显示内容的方法详解
2023-05-31 17:06:38
numpy的squeeze函数使用方法
2022-04-15 10:02:07
![](https://img.aspxhome.com/file/2023/3/75083_0s.png)
python使用cPickle模块序列化实例
2022-06-26 22:35:09
百度首席设计师 用户体验部总监郭宇演讲
2008-09-03 12:41:00
用数据库生成不重复的流水号
2007-10-06 22:49:00
监测站点使用多少session和application的asp程序
2007-09-12 19:40:00
Yii框架实现乐观锁与悲观锁流程详解
2023-11-16 13:38:38
后工业时代的后规范思考2——如何应用
2009-06-05 12:35:00
css+JavaScript实现PDF、ZIP、DOC链接的标注
2007-05-11 17:03:00
Python实现操作Redis的高级用法分享
2022-07-29 04:26:29
利用Python实现自动扫雷小脚本
2023-05-10 01:45:27
![](https://img.aspxhome.com/file/2023/7/84977_0s.jpg)
Python super()方法原理详解
2023-06-19 18:28:30
ASP 常见对象总结(熟悉一下利用以后的开发使用)
2011-03-07 10:57:00
MySQL中随机生成固定长度字符串的方法
2010-12-08 16:25:00
用"表情符号"做植入广告 是否可行呢?
2009-02-23 13:07:00