写我小小的日志系统

配置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日志系统的更多相关文章

  1. Python logging(日志)模块

    python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...

  2. 【python】日志系统

    来源: http://blog.csdn.net/wykgf/article/details/11576721 http://www.jb51.net/article/42626.htm http:/ ...

  3. Python logging日志管理

    import logging logger = logging.getLogger("simple_example") logger.setLevel(logging.DEBUG) ...

  4. python logging 日志轮转文件不删除问题

    前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...

  5. python logging 日志轮转文件不删除问题的解决方法

    项目使用了 logging 的 TimedRotatingFileHandler : #!/user/bin/env python # -*- coding: utf-8 -*- import log ...

  6. Python脚本日志系统

    Python通过logging模块提供日志功能,关于logging模块的使用网络上已经有很多详细的资料,这里要分享的是怎样在实际工程中使用日志功能. 假设要开发一个自动化脚本工具,工程结构如下,Com ...

  7. python logging日志模块

    一.logging模块的简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不 ...

  8. 管理 python logging 日志使用

    1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上INFO:确认一切按预期运行WA ...

  9. python logging 日志

    logging与print 区别,为什么需要logging? 在写脚本的过程中,为了调试程序,我们往往会写很多print打印输出以便用于验证,验证正确后往往会注释掉,一旦验证的地方比较多,再一一注释比 ...

随机推荐

  1. Python_if

    if if c语言中的if语句格式如下: if (条件) { 结果} python的格式与其不同,定义了自己的格式,更加的简明: if 条件 : 结果 print(111) if 3 > 2: ...

  2. Git操作记录

    记录一些用过的操作 增加远程推送分支 git remote add orgin http://xxxxx.git 直接新建本地分支,将远程分支提取出来. git checkout -t origin/ ...

  3. vue省市区三级联动

    仿照小米之家做的一个省市区三级联动,先上代码: HTML: <template> <section class="myAddress"> <secti ...

  4. git bash 连接github并提交项目工程

    借鉴博客:https://www.cnblogs.com/flora5/p/7152556.html https://blog.csdn.net/heng_yan/article/details/79 ...

  5. Http和Https有什么区别

    以前去面试的时候,好几家公司都会问到这个问题:http和https有什么区别? 最近突然想恶补一些基础,再夯实一下自己实力,毕竟强大的能力才是工资的保证嘛,今天就来简单记录一下htttp和https的 ...

  6. MariaDB多实例的安装配置

    初始化数据库: mysql_install_db  --basedir=/var/lib/mysql --datadir=/data/3306/data --user=mysql mysql_inst ...

  7. 2018 API变化

  8. 特殊计数序列——第二类斯特林(stirling)数

    计算式 \[ S(n,m)=S(n-1,m-1)+mS(n,m) \] \(S(0,0)=1,S(i,0)=0(i>0)\) 组合意义 将\(n\)个不可分辨的小球放入\(m\)个不可分辨的盒子 ...

  9. 【XSY3344】连续段 DP 牛顿迭代 NTT

    题目大意 对于一个长度为 \(n\) 的排列 \(p\),我们称一个区间 \([l,r]\) 是连续的当且仅当 \((\max_{l\leq i\leq r}a_i)-(\min_{l\leq i\l ...

  10. 一本通 一笔画问题 洛谷P1636 Einstein学画画

    P1636 Einstein学画画 相信大家都玩过一笔画这种游戏吧,这其实算得上是我们能够接触到的比较常见的数学问题,有一个很知名的就是七桥问题 这个问题包括所有的一笔画问题都是在欧拉回路的涵盖范围内 ...