python logging模块日志回滚RotatingFileHandler
# coding=utf-8
import logging
import time
import os
import logging.handlers def logger(appname,rootstdout=True): log_fmt= "%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
c_fmt="%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
date_format = "%Y-%m-%d %H:%M:%S %a"
#设置Console输出level
logging.basicConfig(level=logging.DEBUG,
format=c_fmt,
datefmt=date_format, ) list_level=["Error","Info","Warning","Debug"]
stamp = time.strftime("%Y%m%d", time.localtime())+".log"
logsdir=os.path.join(os.getcwd(),"logs")
if os.path.exists(logsdir):
for p in list_level:
if os.path.exists(os.path.join(logsdir,p)):
pass
else:
os.mkdir(os.path.join(logsdir,p))
else:
os.mkdir(logsdir)
list_level=["Error","Info","Warning","Debug"]
for p in list_level:
print(os.path.join(logsdir,p))
os.mkdir(os.path.join(logsdir,p)) f_dict={}
for i in list_level:
filename=os.path.join(logsdir,i,stamp)
f_dict[i]=filename
logger= logging.getLogger(root) for k,v in f_dict.items():
handler=logging.handlers.RotatingFileHandler(filename=v, maxBytes=1024*1024*50, backupCount=5,encoding="utf-8",delay=False)
h_fmt=logging.Formatter(log_fmt)
handler.setFormatter(h_fmt)
if k==list_level[0]:
handler.setLevel(logging.ERROR)
elif k==list_level[1]:
handler.setLevel(logging.INFO)
elif k== list_level[2]:
handler.setLevel(logging.WARNING)
else :
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.propagate = rootstdout
return logger if __name__ == "__main__":
logger=logger("root",rootstdout=True)
while True:
time.sleep(0.01)
logger.info("file test",exc_info=True)
logger.debug("bebug test")
logger.error("error test")
logger.warning("warning test") C:\Python37\python.exe C:/Users/Administrator/PycharmProjects/Supro/src/rotating_rewrite.py
2019-03-21 23:32:40 Thu --SshTest.class [INFO]:
file test
NoneType: None
2019-03-21 23:32:40 Thu --SshTest.class [DEBUG]:
bebug test
2019-03-21 23:32:40 Thu --SshTest.class [ERROR]:
error test
2019-03-21 23:32:40 Thu --SshTest.class [WARNING]:
warning test
2019-03-21 23:32:40 Thu --SshTest.class [INFO]:
file test
NoneType: None
2019-03-21 23:32:40 Thu --SshTest.class [DEBUG]:
python logging模块日志回滚RotatingFileHandler的更多相关文章
- python logging模块日志回滚TimedRotatingFileHandler
# coding=utf-8 import logging import time import os import logging.handlers import re def logger(app ...
- Python logging模块日志存储位置踩坑
问题描述 项目过程中写了一个小模块,设计到了日志存储的问题,结果发现了个小问题. 代码结构如下: db.py run.py 其中db.py是操作数据库抽象出来的一个类,run.py是业务逻辑代码.两个 ...
- python logging模块日志输出
import logging logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) handler = ...
- 日志回滚:python(日志分割)
日志回滚:python 什么是日志回滚? 答: 将日志信息输出到一个单一的文件中,随着应用程序的持续使用,该日志文件会越来越庞大,进而影响系统的性能.因此,有必要对日志文件按某种条件进行切分,要切分日 ...
- Python logging模块使用记录
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...
- Python logging模块简介
logging模块提供logger,handler,filter,formatter. logger:提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过logg ...
- python logging模块小记
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...
- Python logging模块无法正常输出日志
废话少说,先上代码 File:logger.conf [formatters] keys=default [formatter_default] format=%(asctime)s - %(name ...
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
随机推荐
- opencv —— copyMakeBorder 扩充图像边界
扩充图像边界:copyMakeBorder 函数 在图像处理过程中,因为卷积算子有一定大小,所以就会导致图像一定范围的边界不能被处理,这时就需要将边界进行适当扩充. void copyMakeBord ...
- 【python基础语法】运算符&if条件语句&while循环&for循环(第5天课堂笔记)
""" if语法规则: if 条件比较语句1: # 条件成立的时候 会执行的代码 elif 条件比较语句2: # 条件2成立的时候 会执行的代码 else: # 条件不成 ...
- gdck01
有好几年 好几年 好几年 我成天在抱怨 我没钱 我没钱 恨老天不开眼 不开眼 不开眼 为什么好的事 都跟我没有缘
- 微信小程序后端开发流程
微信小程序后端开发流程根据官网总结为两个步骤 1.前端调用 wx.login 返回了code,然后调用wx.getUserInfo获取到用户的昵称 头像 2.服务端根据code去微信获取openid, ...
- 我的第一个原生Web Components——滑块(SingleSlider)
写着写着,就会跑偏,没错又走上了一个岔道……就是不知道这条岔道以后会不会越来越宽,有的说他是未来,有的说…… 这里不知道,也不做什么评断.减少一些重复性的工作,提高开发效率这是最根本的.说白了就是偷懒 ...
- pod has unbound immediate PersistentVolumeClaims : statefulset挂载不上pv的另一种情况
大家都直到当volumeClaimTemplates匹配不上pv时,会出现statefulset挂载不到pv的问题.错误提示如下: error while running "VolumeBi ...
- SendMessage模拟按键所需的虚拟键码
Virtual-Key Codes The following table shows the symbolic constant names, hexadecimal values, and mou ...
- Ubuntu系统测评
首次使用ubuntun系统 华为云可以免费试用30天,嘻嘻,正好熟悉一下linux命令 1.登录 login: 先输入用户名:root 在输入密码:******** 这个是在配置云服务器的时候自己设置 ...
- 插入jupyter notebook代码
<iframe src="https://nbviewer.jupyter.org/gist/gaowenxin95/53408e0f1ce268430efaad2cb1f0ca4f& ...
- shell-快速抽样
有时我们需要对文件进行抽样,这时候只需要一个shell命令就可以抽取固定行数的样本:shuf shuf -n $m $file 参数有2: -n: 抽样行数 -r: 是否重复