# 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的更多相关文章

  1. python logging模块日志回滚RotatingFileHandler

    # coding=utf-8 import logging import time import os import logging.handlers def logger(appname,roots ...

  2. Python logging模块日志存储位置踩坑

    问题描述 项目过程中写了一个小模块,设计到了日志存储的问题,结果发现了个小问题. 代码结构如下: db.py run.py 其中db.py是操作数据库抽象出来的一个类,run.py是业务逻辑代码.两个 ...

  3. python logging模块日志输出

    import logging logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) handler = ...

  4. 日志回滚:python(日志分割)

    日志回滚:python 什么是日志回滚? 答: 将日志信息输出到一个单一的文件中,随着应用程序的持续使用,该日志文件会越来越庞大,进而影响系统的性能.因此,有必要对日志文件按某种条件进行切分,要切分日 ...

  5. Python logging模块使用记录

    1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...

  6. Python logging模块简介

    logging模块提供logger,handler,filter,formatter. logger:提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过logg ...

  7. python logging模块小记

    1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...

  8. Python logging模块无法正常输出日志

    废话少说,先上代码 File:logger.conf [formatters] keys=default [formatter_default] format=%(asctime)s - %(name ...

  9. python logging模块可能会令人困惑的地方

    python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...

随机推荐

  1. 一文告诉你git如何使用

    提供简易教程学习网址 http://www.bootcss.com/p/git-guide/ git add . //提交至缓存 git commit -m '注释' //提交至本地 (git com ...

  2. YII 用gii生成modules模块下的mvc

    1.生成model ModelPath设置为: application.modules.[moduleName].models 2.生成CURD ModelClass设置为: application. ...

  3. AJAX-XMLHttpRequest和本地文件

    网页中可以使用相对URL的能力通常意味着我们能使用本地文件系统来开发和测试HTML,并避免对Web服务器进行不必要的部署. 然而当使用XMLHttpRequest进行Ajax编程时,这通常是不行的. ...

  4. springMVC定时任务总是执行两次

    情况: springmvc的定时任务在本机上测试的时候没有问题,但是放到测试服务器上的时候总是执行两次: 探索:(网上搜索) 一.spring注入的时候实例化了多次,说是spring-servlet. ...

  5. 慕课网 javascript深入浅出编程练习

    任务 请在index.html文件中,编写arraysSimilar函数,实现判断传入的两个数组是否相似.具体需求: 1. 数组中的成员类型相同,顺序可以不同.例如[1, true] 与 [false ...

  6. js面向对象设计之class继承

    EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建立在旧的原型系统上的语法糖,所以它们并没有带来任何的新特性 ...

  7. Dynamics 365Online 查询Web Api的请求WebUri

    在on-premises版本中,获取weburi的方式是进设置-自定义项-开发人员资源中查看地址,但online版本中的地址会有些许的差异 online的开发者资源中的地址如下图,如果你在页面java ...

  8. ifup / ifdown eth0 / eno1 reports unknown interface when it exists!

    li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-lati ...

  9. AR中的SLAM(一)

    写在前面 本系列打算讲讲个人对AR行业和AR中的SLAM算法的一点浅显的看法.才疏学浅,文中必然有很多疏漏和不足,还望能和大家多多讨论.今天先讲讲我对AR的一些认识. AR的一点理解 AR是什么 AR ...

  10. centos7 部署 汉化版 gitlab 10.0.2

    更新说明: 20171009:增加3.5的内容 20171008:整理出gitlab部署手册 =============================================== gitla ...