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

  1. python logging模块日志回滚TimedRotatingFileHandler

    # coding=utf-8 import logging import time import os import logging.handlers import re def logger(app ...

  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. opencv二值化的cv2.threshold函数

    (一)简单阈值 简单阈值当然是最简单,选取一个全局阈值,然后就把整幅图像分成了非黑即白的二值图像了.函数为cv2.threshold() 这个函数有四个参数,第一个原图像,第二个进行分类的阈值,第三个 ...

  2. tensorflow 中的L1和L2正则化

    import tensorflow as tf weights = tf.constant([[1.0, -2.0],[-3.0 , 4.0]]) >>> sess.run(tf.c ...

  3. 数据结构(集合)学习之Map(一)

    集合 框架关系图: 补充:HashTable父类是Dictionary,不是AbstractMap. Map: Map(接口)和Collection都属于集合,但是Map不是Collection的子类 ...

  4. private、public、this关键字

    private关键字 概念:私有的,一种权限修饰符,用来修饰类的成员 特点:被修饰的成员只能在本类中访问 用法: - 1. private 数据类型 变量名: - 2. private 返回值类型 方 ...

  5. SQL Server远程数据库操作(备份、还原等)

    · SQL Server远程数据库备份到本地: exp sauser/sapassword@192.168.8.233:1433/DBName file=d:/backup.dmp OWNER=sum ...

  6. ES6 class(基本语法+方法)

    静态属性与静态方法 1. 不会被类实例所拥有的属性与方法 只是类自身拥有2. 只能通过类调用 静态方法与普通方法重名,不会冲突static 关键字(静态方法) 静态属性类名.属性名 = 属性值; 1. ...

  7. lvs使用进阶

    之前lvs基础篇(https://www.cnblogs.com/ckh2014/p/10855002.html)中介绍了lvs-dr的搭建,下面我们再复习一下,架构如下: 相关配置 director ...

  8. 133.在django中使用memcached

    1. 在django中使用memcached,可以在settings.py文件中DATABASES变量下面配置CACHES缓存相关配置信息,只允许本机连接memcached就可以设置LOCATION为 ...

  9. react-native构建基本页面6---打包发布

    签名打包发布Release版本的apk安装包 请参考以下两篇文章: ReactNative之Android打包APK方法(趟坑过程) React Native发布APP之签名打包APK 如何发布一个a ...

  10. Qt Installer Framework翻译(8)

    好了,到这里翻译就结束了.各位可以下载源码,结合examples示例,使用repogen和binarycreator好好实操一下,就能掌握基础用法了.祝各位使用顺利. 官方文档网址:https://d ...