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打印输出以便用于验证,验证正确后往往会注释掉,一旦验证的地方比较多,再一一注释比 ...
随机推荐
- Angular 基本内置服务和筛选器
AngularJS中的内置服务(共30多个): $http 发送http请求,主要用于进行异步数据请求的功能实现,这个服务主要封装了XMLHttpRequest对象和JSONP数据访问模式来完成远程请 ...
- React笔记:组件(3)
1. 组件定义 组件是React的核心概念,组件将应用的UI拆分成独立的.可复用的模块. 定义组件的两种方式: (1)类组件:使用ES6 class (2)函数组件:使用函数 使用class定义组件的 ...
- 一、查看MVC4还是MVC5
一.查看MVC版本找到那个dll.属性.就可以看到版本 二.MVC添加WebAPI Visual Studio 已向项目“Web”添加 ASP.NET Web API 2 的 全部集合 个依赖项. 项 ...
- parcel+vue入门
一.parcel简单使用 npm install -D parcel-bundler npm init -y (-y表示yes,跳过项目初始化提问阶段,直接生成package.json 文件.) Pa ...
- noip2017部分题目
D1T3 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张NN个点MM条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,NN号点是公园的出口,每条边有一个非负权值, 代表策策经过 ...
- 第一模块:Python基础(二)
目录 1.变量 常量 2.用户交互和注释 程序交互 注释 字符串 布尔型(bool) 格式化输出 运算符 while 循环 @(开发基础) 1.变量 变量用于存储要在计算机程序中引用和操作的信息.它们 ...
- gcc编译C源文件
gcc编译C程序的主要过程是:预处理---编译---汇编---连接,其中:(以名为hello.c的源文件为例) 预处理:对各种预处理指令(#开头,如#include,#define)进行处理,以及删除 ...
- 2018-2019-2 20165232 《网络对抗技术》 Exp6 信息搜集与漏洞扫描
2018-2019-2 20165232 <网络对抗技术> Exp6 信息搜集与漏洞扫描 一.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实践内容. 各种搜索技巧的应 D ...
- spring boot集成FastDFS
官方文档:https://github.com/happyfish100/fastdfs-client-java 一.首先,maven工程添加依赖 <!--fastdfs--> <d ...
- JGUI源码:实现图标按钮及下拉菜单(16)
效果如下 代码片段如下 <div class="jgui-btn" id="personalbtn" style="float:right;&q ...