Python logging日志系统
写我小小的日志系统
配置logging有以下几种方式:
1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数;
2)创建一个日志配置文件,标签式的注明【loggers】、【handlers】、【formatters】、【filters】4大组件,前3者必传,后者选传,然后使用fileConfig()函数来读取该文件的内容;
3)创建一个包含【loggers】、【handlers】、【formatters】、【filters】4大组件配置信息的字典dict,然后把它传递给dictConfig()函数;
我目前采用的就是第3种方式。
# -*- coding: utf-8 -*-
import logging
from flask import Flask
from logging.config import dictConfig app = Flask(__name__) dictConfig({
'version': 1,
'formatters': {
'standard': {
'format': '%(asctime)s.%(msecs)d|%(thread)d|%(levelname)s|%(message)s'
, 'datefmt': '%Y-%m-%d %H:%M:%S'}
, 'detail': {
'format': '%(asctime)s.%(msecs)d|%(thread)d|%(levelname)s|%(filename)s:%(funcName)s line %(lineno)d'
, 'datefmt': '%Y-%m-%d %H:%M:%S'
},
},
'filters': {
},
'handlers': {
'default': {
'class': 'logging.handlers.RotatingFileHandler', # 将日志消息发送到磁盘文件,并支持日志文件按大小切割
'filename': '../logs/info.log', # 日志输出文件
'maxBytes': 1024 * 1024 * 5, # 文件大小
'formatter': 'standard', # 使用哪种formatters日志格式
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'standard'
},
'error': {
'level': 'ERROR',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '../logs/error.log',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 5, # 备份份数
'formatter': 'detail',
},
'request_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '../logs/script.log',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 5,
'formatter': 'standard',
}
},
'loggers': {
'flask': {
'handlers': ['default', 'console', 'error'],
'level': 'DEBUG',
'propagate': True
},
'flask.request': {
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': False,
},
'weTest.flask': {
'handlers': ['error'],
'level': 'ERROR',
'propagate': True
}
}
}) logger = logging.getLogger('flask')
logger.setLevel(logging.DEBUG) if __name__ == '__main__':
try:
logger.info('info info')
logger.debug('debug info')
print 1 / 0
except Exception as err:
logger.error('error message:{0}'.format(err.message), exc_info=True) # 将异常异常信息添加到日志消息中
其他.py文件中应用
# test.py from utils.log_helper import logger try:
logger.info('hello world')
print 1/0
except Exception as err:
logger.error('error message:{0}'.format(err.message), exc_info=True)
参考&&转载
Python logging 官网 https://docs.python.org/2/library/logging.html
博文 https://www.cnblogs.com/yyds/p/6901864.html
Python logging日志系统的更多相关文章
- Python logging(日志)模块
python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...
- 【python】日志系统
来源: http://blog.csdn.net/wykgf/article/details/11576721 http://www.jb51.net/article/42626.htm http:/ ...
- Python logging日志管理
import logging logger = logging.getLogger("simple_example") logger.setLevel(logging.DEBUG) ...
- python logging 日志轮转文件不删除问题
前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...
- python logging 日志轮转文件不删除问题的解决方法
项目使用了 logging 的 TimedRotatingFileHandler : #!/user/bin/env python # -*- coding: utf-8 -*- import log ...
- Python脚本日志系统
Python通过logging模块提供日志功能,关于logging模块的使用网络上已经有很多详细的资料,这里要分享的是怎样在实际工程中使用日志功能. 假设要开发一个自动化脚本工具,工程结构如下,Com ...
- python logging日志模块
一.logging模块的简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不 ...
- 管理 python logging 日志使用
1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上INFO:确认一切按预期运行WA ...
- python logging 日志
logging与print 区别,为什么需要logging? 在写脚本的过程中,为了调试程序,我们往往会写很多print打印输出以便用于验证,验证正确后往往会注释掉,一旦验证的地方比较多,再一一注释比 ...
随机推荐
- Index-技术学习系列博客
计算机理论基础系列 B树的插入操作 前端系列 安装nodejs和webpack环境 构建vue项目 Json Web Token VO和DO转换(一) 工具汇总 缓存的实现和使用 框架学习系列 shi ...
- Net包管理NuGet(1)nuget的使用方法
关于nuget,有很多介绍想要深入了解的可以看看官网https://docs.microsoft.com/zh-cn/nuget/what-is-nuget 本文简单介绍让不知道的可以快速了解 1,使 ...
- HBuilderX——编译失败:HBuilderX 安装目录不能包括 ( 等特殊字符
前言 编译小程序的时候报错,原因其实很简单,安装目录的问题! 解决 我安装到了D:\Program Files (x86),放到D盘的根目录下或者D:\Program Files只要不包含一些特殊字符 ...
- ECMA262,JavaScript引擎,浏览器
相关阅读:https://www.cnblogs.com/970119449blog/p/8080133.html 相关阅读:https://www.jb51.net/article/75888.ht ...
- SQL server 行转列 列转行
1.简单案例 create database Hang go use Hang create table Students ( Name varchar(50), Kemu varchar(50), ...
- cookie-闲聊
最近练习时对cookie接触较多,所以就着cookie的Domain与path属性闲聊几句. 首先,对于cookie要明确,cookie可以由自身属性确定哪些站点可以看到相应的cookie.毕竟一个浏 ...
- linux的sed命令(一)
转自:https://www.cnblogs.com/ginvip/p/6376049.html Sed 简介 sed 是一种新型的,非交互式的编辑器.它能执行与编辑器 vi 和 ex 相同的编辑任务 ...
- cmake 递归依赖
现在有3个模块:main.service.base,main依赖service的service.h.service依赖base的base.h,怎么写CMakeList.txt避免main直接耦合bas ...
- day22 栈 , 队列 , 约束和反射
#!/usr/bin/env python# -*- coding:utf-8 -*- # 1.请使用面向对象实现栈(后进先出)"""class Account: def ...
- [Reinforcement Learning] Cross-entropy Method
Cross-entropy Method(简称CEM)虽然是一种基于交叉熵的算法,但并不是我们熟知的监督学习中的交叉熵方法,与其说它是一种基于交叉熵的算法,倒不如说是一种基于蒙特卡洛和进化策略的算法. ...