python - logging.basicConfig format参数无效
有这么一段python代码
import threading
import time
import requests
from decimal import Decimal, ROUND_DOWN
import logging
import os
import sys
import randomfrom utils import common, filter, cache
from configs import settings logging.basicConfig(level=logging.INFO, format='%(levelname)s %(asctime)s [line:%(lineno)d] %(message)s')
不管怎么设置basicConfig里的值,一直都无法生效,后来看到一个说法:在调用basicConfig函数之前,因为导入了其他包,而其他包里又导入了logging包,就导致设置basicConfig不成功。一排查,确实在common和cache包里又导入了logging。
调整代码顺序,如下:
import os
import sys
import random
import threading
import time
import requests
from decimal import Decimal, ROUND_DOWN
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s %(asctime)s [line:%(lineno)d] %(message)s') this_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(this_dir, '..'))
from utils import common, filter, cache
from configs import settings
确实,就生效了。
经排查,“在调用basicConfig函数之前,因为导入了其他包,而其他包里又导入了logging包,就导致设置basicConfig不成功” 这个说法还不够,应该是 “在调用basicConfig函数之前,因为导入了其他包,而其他包里又导入了logging包,且也调用了basicConfig函数,就导致设置basicConfig不成功”。
为什么呢?上 basicConfig 源码:
def basicConfig(**kwargs):
_acquireLock()
try:
if len(root.handlers) == 0:
filename = kwargs.get("filename")
if filename:
mode = kwargs.get("filemode", 'a')
hdlr = FileHandler(filename, mode)
else:
stream = kwargs.get("stream")
hdlr = StreamHandler(stream)
fs = kwargs.get("format", BASIC_FORMAT)
dfs = kwargs.get("datefmt", None)
fmt = Formatter(fs, dfs)
hdlr.setFormatter(fmt)
root.addHandler(hdlr)
level = kwargs.get("level")
if level is not None:
root.setLevel(level)
finally:
_releaseLock()
因为,在其他地方已经调用过了basicConfig函数,在当前文件中再调用basicConfig的时候,会发现 len(root.handlers) 的长度已经不再为0了,所以导致不走 if len(root.handlers) == 0,所以设置的日志格式无效。
python - logging.basicConfig format参数无效的更多相关文章
- Python Logging模块的简单使用
前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...
- python Logging的使用
日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分. 根据事件的轻重可分为以下几个级别: DEBUG: 详细信息, ...
- python logging method 02
基本用法 下面的代码展示了logging最基本的用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- 管理 python logging 日志使用
1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上INFO:确认一切按预期运行WA ...
- Python logging(日志)模块
python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...
- Python logging 模块简介
Table of Contents 1. Logging 模块 1.1. 简介 1.2. 简单输出日志 1.3. 输入日志到文件 1.4. 几个基本概念 1.4.1. loggers 1.4.2. h ...
- python logging模块学习(转)
前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...
- python logging 日志使用
https://docs.python.org/3/library/logging.html1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRI ...
- python logging 总结
基本用法: import sys # 获取logger实例,如果参数为空则返回root logger logger = logging.getLogger("AppName" ...
随机推荐
- 攻防世界高手进阶之Web_python_block_chain(2018年DDCTFmini blockchain)
打开题目大概看了一下,是有关区块链的题目, 感觉代码要格式化一下,不然没法看 代码格式化站点:https://www.html.cn/tool/js_beautify/ hash of genesis ...
- CentOS7.0 内核(3.10.0-123.el7.x86_64)bug导致KVM物理机重启
一.问题描述 服务器硬件:DELL R720 系统版本:CentOS7.0 内核版本:3.10.0-123.el7.x86_64 故障现象:偶尔会重启 二.问题原因 经查看dmesg日志发现是kern ...
- matplotlib--基本setting
一.创建自定义图像 figure figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=Tr ...
- 洛谷P3810 陌上花开(CDQ分治)
洛谷P3810 陌上花开 传送门 题解: CDQ分治模板题. 一维排序,二维归并,三维树状数组. 核心思想是分治,即计算左边区间对右边区间的影响. 代码如下: #include <bits/st ...
- 20、Python常用模块sys、random、hashlib、logging
一.sys运行时环境模块 sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. 用法: sys.argv:命令行参数List,第一个元素是程序本身 ...
- (21) 树莓派使用python调用命令行 python中调用linux命令及os.system的返回值
cmd = "sudo shutdown -h now"; os.system(cmd)
- luoguP3374 【模板】树状数组 1 cdq
链接 luogu 思路 可耐我连cdq都不会,Orz 陈丹琦 代码 #include <bits/stdc++.h> using namespace std; const int N = ...
- cogs 998. [東方S2] 帕秋莉·诺蕾姬
二次联通门 : cogs 998. [東方S2] 帕秋莉·诺蕾姬 交上去后发现自己没上榜 就想着加点黑科技 把循环展开一下 结果WA了.. 万恶的姆Q /* cogs 998. [東方S2] 帕秋莉· ...
- JAVA基础--MySQL(二)
数据库约束 1.基础限制 ① 单一表内字节量总和不能超过65535,null 占用一个字节空间 ② varchar存储255 以内字节占用一个字节表示长度,255以上自己则占用两个字节表示长度 ③ ...
- 一起学Makefile(一)
make和makefile makefile文件帮助我们记录了整个项目工程的所有需要编译的文件列表,这样我们在编译时仅需要输入简单的make命令就能编译出我们期望的结果. makefile文件反映了整 ...