0x02 Python logging模块利用配置加载logger
logging模块利用配置加载logger
logging.config模块提供了从配置加载创建logger等相关对象,并放入manager对象中进行缓存待用。所以记录下一般几种方式配置的范本模式,方便项目中copy直接修改使用。
dict config references 官档关于logging配置字典说明
方式一模板:logging.config.dictConfig(config_dict)
config_dict 字典模板
cfg = {
'version': 1,
'formatters': {
'detailed': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
},
'file': {
'class': 'logging.FileHandler',
'filename': 'mplog.log',
'mode': 'w',
'formatter': 'detailed',
},
'foofile': {
'class': 'logging.FileHandler',
'filename': 'mplog-foo.log',
'mode': 'w',
'formatter': 'detailed',
},
'errors': {
'class': 'logging.FileHandler',
'filename': 'mplog-errors.log',
'mode': 'w',
'level': 'ERROR',
'formatter': 'detailed',
},
},
'loggers': {
'foo': {
'handlers': ['foofile']
}
},
'root': {
'level': 'DEBUG',
'handlers': ['console', 'file', 'errors']
},
}
模板二:
LOGGER_CONFIG_DICT = {
'version': 1,
'formatters': {
'detailed_fmt': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(created)s %(levelname)-6s %(name)-15s %(processName)s:%(threadName)s %(message)s'
# human-readable timestamp levelname logger_name processname threadname message
},
'simple_fmt': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
# human-readable levelname logger_name message
},
'portal_fmt': {
'class': 'logging.Formatter',
# 'datefmt': '%Y-%m-%d %H:%M:%S,uuu', # 实际默认格式就是这个
'format': '%(asctime)s %(levelname)-8s %(name)-15s %(processName)-10s %(message)s'
},
'system_fmt': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
},
},
'handlers': {
# 'console_hd': {
# 'class': 'logging.StreamHandler',
# 'level': 'INFO'
# },
'time_rotate_file_hd': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': './logs/selfservices1.log',
'when': 'M',
'interval': 5,
'backupCount': 100,
'formatter': 'simple_fmt'
},
'file_size_rotate_hd': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': './logs/portal.log',
'mode': 'a',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 50,
'formatter': 'portal_fmt'
},
'errors_hd': {
'class': 'logging.FileHandler',
'filename': './logs/errors.log',
'formatter': 'detailed_fmt',
'level': 'ERROR' # 只会错误40及以上的日志
},
'system_hd': {
'class': 'logging.FileHandler',
'filename': './logs/system.log',
'formatter': 'simple_fmt'
}
},
'loggers': {
'selfservices': {
'level': 'INFO',
'handlers': ['file_size_rotate_hd', 'errors_hd']
# 'handlers': ['time_rotate_file_hd', 'file_size_rotate_hd', 'errors_hd']
}
},
'root': {
'level': 'DEBUG',
'handlers': ['system_hd', 'errors_hd']
},
}
0x02 Python logging模块利用配置加载logger的更多相关文章
- 0x01 Python logging模块
目录 Python logging 模块 前言 logging模块提供的特性 logging模块的设计过程 logger的继承 logger在逻辑上的继承结构 logging.basicConfig( ...
- 基于python的opcode优化和模块按需加载机制研究(学习与个人思路)(原创)
基于python的opcode优化和模块按需加载机制研究(学习与思考) 姓名:XXX 学校信息:XXX 主用编程语言:python3.5 个人技术博客:http://www.cnblogs.com/M ...
- 利用ChromeOptions()加载用户配置
一. 如何绕过页面登录 我们在登录网站的时候,通常需要输入用户名.密码和验证码,那么有没有办法绕过登录环节呢? 有两种方法可以解决这个问题,一种是利用chrome浏览器的用户配置,一种是利用cooki ...
- Python logging模块无法正常输出日志
废话少说,先上代码 File:logger.conf [formatters] keys=default [formatter_default] format=%(asctime)s - %(name ...
- (转)python logging模块
python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...
- 读懂掌握 Python logging 模块源码 (附带一些 example)
搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...
- Python logging 模块学习
logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...
- Python logging 模块简介
Table of Contents 1. Logging 模块 1.1. 简介 1.2. 简单输出日志 1.3. 输入日志到文件 1.4. 几个基本概念 1.4.1. loggers 1.4.2. h ...
- Log4j2源码分析系列:(一)配置加载
前言 在实际开发项目中,日志永远是一个绕不开的话题.本系列文章试图以slf4j和log4j2日志体系为例,从源码角度分析日志工作原理. 学习日志框架,首先要熟悉各类日志框架,这里推荐两篇文章,就不再赘 ...
随机推荐
- DLinNLP
2015蒙特利尔深度学习暑期学校之自然语言处理篇 用户1737318 8月3日至8月12日在蒙特利尔举办的深度学习署期学校中,来自不同领域的深度学习顶尖学者 (Yoshua Bengio, Leon ...
- 在WEB显示实时视频流
转载自:https://www.jianshu.com/p/7ef5490fbef7 安装摄像头 这里使用的是树莓派的官方摄像头,使用普通的 USB 摄像头也可以,但前提是你能够搞的定它的驱动. 大概 ...
- [题解向] 正睿Round435
10.14 Link 唔,这一场打得不好.获得了\(\rm 75pts/300pts\)的得分,但是居然可以获得\(\rm 27/69\)的名次,也不至于不满意--毕竟是真不会233 \(\rm T1 ...
- [LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- Sentinel Client: 整合Apollo规则持久化
在前面的学习过程中,Sentinel 的规则,也就是我们之前定义的限流规则,是通过代码的方式定义好的.这是初始化时需要做的事情,Sentinel 提供了基于API的方式修改规则: FlowRuleMa ...
- java 声明并初始化整型变量
public class Sample { public static void main(String[] args) { int num; num = ; System.out.println(& ...
- Leetcode 第137场周赛解题报告
今天的比赛的题目相对来说比较「直白」,不像前几周都是一些特定的算法,如果你没学过不可能想出来. 做了这些周,对leetcode比赛的题目也发现了一些「规律」. 一般前两道题都很「简单」,只要有想法,直 ...
- Scala 定义复杂枚举
通常我们需要枚举参数不止两个,Scala提供的枚举类最多定义两个参数:id:Int与name:String.不能满足我们通常要求. object BaseEntryEnum extends Enume ...
- layui.dropdown.js
前 在 layui 框架下做了一个小组件,是下拉框功能,当然也可以很好的变成其他组件,前提你会修改. 还需要更多的完善.后期(我也不清楚会是啥时候会优化)