python logging模块日志回滚TimedRotatingFileHandler
# coding=utf-8
import logging
import time
import os
import logging.handlers
import re
def logger(appname,rootstdout=True): log_fmt= "%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
c_fmt="%(asctime)s --%(name)s %(filename)s.%(funcName)s():%(lineno)d [%(levelname)s]:\n%(message)s"
date_format = "%Y-%m-%d %H:%M:%S %a"
# 设置控制台输出level
logging.basicConfig(level=logging.DEBUG,
format=c_fmt,
datefmt=date_format, ) list_level=["Error","Info","Warning","Debug"]
stamp = "dailylog.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(appname) for k,v in f_dict.items():
handler=logging.handlers.TimedRotatingFileHandler(filename=v,when='MIDNIGHT', interval=1, backupCount=4)
handler.suffix = "%Y-%m-%d.log"
handler.extMatch = r"^\d{4}-\d{2}-\d{2}.log$"
handler.extMatch = re.compile(handler.extMatch)
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)
#是否控制台输出stdout
logger.propagate = rootstdout
return logger
if __name__ == "__main__":
logger=logger("root",rootstdout=True) time.sleep(0.01)
try:
assert 1==2
except Exception as e:
# exc_info用来抛出异常到控制台或日志文件
logger.info("ddd",exc_info=True) logger.debug("bebug test")
logger.error("error test")
logger.warning("warning test")
2019-03-21 23:27:52 Thu --root timeromat.py.<module>():67 [INFO]:
ddd
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/Supro/src/timeromat.py", line 65, in <module>
assert 1==2
AssertionError
2019-03-21 23:27:52 Thu --root timeromat.py.<module>():69 [DEBUG]:
bebug test
2019-03-21 23:27:52 Thu --root timeromat.py.<module>():70 [ERROR]:
error test
2019-03-21 23:27:52 Thu --root timeromat.py.<module>():71 [WARNING]:
warning test
Process finished with exit code 0
python logging模块日志回滚TimedRotatingFileHandler的更多相关文章
- python logging模块日志回滚RotatingFileHandler
# coding=utf-8 import logging import time import os import logging.handlers def logger(appname,roots ...
- 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文件的调 ...
随机推荐
- display:table-cell自适应布局下连续单词字符换行——张鑫旭
之前有几次提到了使用display:table-cell实现强大的任意尺寸元素的自适应布局(都藏在长长文章之中).这里开篇再次提一下,希望能将该技术普及下去. 典型的双栏布局类名使用如下: fix l ...
- CSS属性之attr()
attr()准确的说,不应该是一个属性,而是一个CSS的函数,我们先看看MDN上的介绍吧: Summary The attr() CSS function is used to retrieve th ...
- iview select下拉bug
1场景:弹框内有一个下拉组件(支持搜索),当选择完数据后弹框关闭,再次打开后,下拉框内的数据是刚才选中的数据.原因:分析后觉得是搜索内容没有清空,导致下拉的数据只有一个解决:调用下setQuery方法 ...
- CVE-2017-11882钓鱼样本构造
前言 漏洞详情: https://embedi.com/blog/skeleton-closet-ms-office-vulnerability-you-didnt-know-about 最近的一个影 ...
- linux客户端打印报表时操作系统的配置
报表打印是用applet方式操作的,所以客户端要有jre环境.如果客户端是windows系统的话,安装jre环境比较方便:如果客户端是linux系统的话,即使服务器端reportConfig.x ...
- canvas验证码 - 随机字母数字
基于canvas制作随机生成数字英文组合验证码效果,点击或刷新会自动重组.输入验证码提交验证效果代码. <div class="verification"> <i ...
- 实现serializable接口的作用
最重要的两个原因是: 1.将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本: 2.按值将对象从一个应用程序域发送至另一个应用程序域. 实现serializable接口的作用是就是可以 ...
- Leetcode题解之Container With Most Water
1.题目描述 2.题目分析 首先,这个题可以使用暴力解法,时间复杂度是O(n^2),这个显然是最容易的做法,但是效率不够高,题目提供了一种解法,使用两个指针,一个从头向尾部,另外一个从尾部向头部,每一 ...
- leetcode 之 Degree of an Array
1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- 《SQL Server 2008从入门到精通》--20180710
目录 1.使用Transact-SQL语言编程 1.1.数据定义语言DDL 1.2.数据操纵语言DML 1.3.数据控制语言DCL 1.4.Transact-SQL语言基础 2.运算符 2.1.算数运 ...