# 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. 原生javascript实现图片自动轮播和点击轮播代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 用Jquery去写树结构

    <!DOCTYPEhtml> <html> <head> <metacharset="utf-8"> <title>Tr ...

  3. css文字属性

    font-family- css字体:设定时,需考虑浏览器中有无该字体. 比如说“黑体”  “微软雅黑” font-size -css字体大小: 注意度量html单位.例如:font-size:18p ...

  4. mac os idea的快捷键

    全局搜索:shift+command+f 搜索类:command+o 光标向前向后移动:command+option+(左/右) 删除一行: command+delete

  5. x86项目中读取注册表Register数据项的方法

    x86项目中使用Registry读取key/value的时候,会出现重定向的问题,解决方法如下: public static string GetMachineGuid() { string guid ...

  6. Linux sar命令工具详细介绍

    sar命令工具详细介绍 by:授客 QQ:1033553122 由于篇幅限制,采用网盘分享, 下载地址: sar命令工具详细介绍.pdf

  7. javascript的继承实现

    javascript虽然是一门面向对象的语言,但是它的继承机制从一开始设计的时候就不同于传统的其他面向对象语言,是基于原型的继承机制,但是在这种机制下,继承依然有一些不同的实现方式. 方法一:类式继承 ...

  8. 为什么不要使用 select * from xxx (oracle 亲测)

    打开已用时间set timing on;create table users(id number(20), name varchar2(20), password varchar2(20));inse ...

  9. Replication--复制Token

    --创建tokenDECLARE @tokenID AS INT;EXEC sys.sp_posttracertoken @publication = @publication,@tracer_tok ...

  10. vue-cli的webpack模板项目配置文件说明

    如果没有vue-cli,那么进行vue项目的开发环境配置将是很费力的一件事情,现在脚手架可以快速构建一个开发环境,是很不错的.不过对于脚手架构建的配置,还是要大概了解一下,方便自己调试配置. 初始化一 ...