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文件的调 ...
随机推荐
- vue.config.js添加路径别名
在组件库中添加配置文件后其它文件需要引用它,此时想到利用路径的别名比较方便,相当于缩写了,请看下面的添加过程: (一)在vue.config.js文件中添加的内容如粗体字体所示: const path ...
- WebDev.WebServer20.exe应用程序错误
我的.net网页,在iis运行成功,在VS2010调试网页时报一个WebDev.WebServer20.exe应用程序错误. 最后查找资料,发现了网站设置的框架是2.0,在vs2010里不能调试2.0 ...
- 通过 Chrome浏览器 查看http请求报文
as we all know HTTP 请求报文 包含请求行.请求头和请求体三部分 请求行:(请求方式 资源路径 协议/版本) 例如:POST /test/index.html HTTP/1.1 P ...
- 基于光盘配置yum源
#开启自动挂载服务 systemctl start autofs #设置开机自动挂载 systemctl enable autofs #光盘自动挂载路径/misc/cd “包含repoda ...
- javaSE学习笔记(11)--- Map
javaSE学习笔记(11)--- Map 1.Map集合 现实生活中,我们常会看到这样的一种集合:IP地址与主机名,身份证号与个人,系统用户名与系统用户对象等,这种一一对应的关系,就叫做映射.Jav ...
- 【spring boot】SpringBoot初学(6)– aop与自定义注解
前言 github: https://github.com/vergilyn/SpringBootDemo 一.AOP 官方demo:https://github.com/spring-project ...
- linux 第一个知识点(my)
关于Linux - rwx rwx rwx root user 194 Oct : 19 21:24 test -:此符号为文件名,如果是d 则为目录,如果是l 则为连接 rwx: 这是所有者所有的 ...
- npx是什么
npx是一个执行npm软件包的二进制文件,通俗的讲,他可以执行npm的一些指令 在npm>5.2.0 的时候npm里面就集成了npx 比如npx webpack 他会执行node_modules ...
- Tomcat中相关配置详解
tomcat的相关配置 server.xml <Server port="8005" shutdown="SHUTDOWN"> <!-- 属性 ...
- (备份)找回 Chrome 配置
C:\Users\xiaogezi.cn\AppData\Local\Google\Chrome\User Data\Default\ 将 Preferences 文件复制到别的地方,用于备份: 将 ...