# 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. D. Domino for Young

    基本思想是利用涂色的方法,用黑白两种颜色把方格全部涂色,相邻方格不同色. 方法1:基于二分图匹配的思想 一开始也想过二分图匹配,但数据量太大,就放弃了这种想法.其实根据增广路的定义.如果白色的方格的数 ...

  2. 如何使用GitHub中和别人合作开发一个项目

    第一步:找到别人在GitHub的项目,点击Fork跳转到自己的GitHub 第二步:跳转到自己页面后进行克隆下载 第三步:在自己的idea中修改完毕后,进行推送将自己GitHub的项目进行修改 第四步 ...

  3. 前向传播和反向传播实战(Tensor)

    前面在mnist中使用了三个非线性层来增加模型复杂度,并通过最小化损失函数来更新参数,下面实用最底层的方式即张量进行前向传播(暂不采用层的概念). 主要注意点如下: · 进行梯度运算时,tensorf ...

  4. [PAT] A1018 Public Bike Management

    [思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...

  5. python高级应用

    一:错误和异常 1.python错误和异常 语法错误:Python的语法错误或者解析错. 异常:运行时检测到的错误被称为异常. 2.异常处理 Python中使用try except 语句来捕获并处理异 ...

  6. Secondary NameNode:它究竟有什么作用?

    前言 最近刚接触Hadoop, 一直没有弄明白NameNode和Secondary NameNode的区别和关系.很多人都认为,Secondary NameNode是NameNode的备份,是为了防止 ...

  7. Navigation Nightmare POJ - 1984 带权并查集

    #include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...

  8. Linux 基础操作命令

    关机和注销 shutdown -h now 立刻关机 shutdown -r now 立刻重启 shutdown -h + 1分钟后关机(重启同样用法) shutdown -h : 11点钟关机(重启 ...

  9. java学习笔记之IO编程—内存流、管道流、随机流

    1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...

  10. react-native构建基本页面4---渲染电影列表

    电影列表 import React, { Component } from 'react' import { View, Image, Text, ActivityIndicator, FlatLis ...