将不同级别的logging 日志信息写入到不同文件

# -*- coding: utf-8 -*-
import os
import time
import logging
import inspect
from logging.handlers import RotatingFileHandler dir = os.path.dirname(__file__)
dir_time = time.strftime('%Y-%m-%d', time.localtime()) handlers = {logging.NOTSET: os.path.join(dir, 'notset_%s.log'%dir_time), logging.DEBUG: os.path.join(dir, 'debug_%s.log'%dir_time), logging.INFO: os.path.join(dir, 'info_%s.log'%dir_time), logging.WARNING: os.path.join(dir, 'warning_%s.log'%dir_time), logging.ERROR: os.path.join(dir, 'error_%s.log'%dir_time), logging.CRITICAL: os.path.join(dir, 'critical_%s.log'%dir_time),
} def createHandlers():
logLevels = handlers.keys() for level in logLevels:
path = os.path.abspath(handlers[level])
handlers[level] = RotatingFileHandler(path, maxBytes=10000, backupCount=2, encoding='utf-8') # 加载模块时创建全局变量 createHandlers() class TNLog(object): def printfNow(self):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) def __init__(self, level=logging.NOTSET):
self.__loggers = {} logLevels = handlers.keys() for level in logLevels:
logger = logging.getLogger(str(level)) # 如果不指定level,获得的handler似乎是同一个handler? logger.addHandler(handlers[level]) logger.setLevel(level) self.__loggers.update({level: logger}) def getLogMessage(self, level, message):
frame, filename, lineNo, functionName, code, unknowField = inspect.stack()[2] '''日志格式:[时间] [类型] [记录代码] 信息''' return "[%s] [%s] [%s - %s - %s] %s" % (self.printfNow(), level, filename, lineNo, functionName, message) def info(self, message):
message = self.getLogMessage("info", message) self.__loggers[logging.INFO].info(message) def error(self, message):
message = self.getLogMessage("error", message) self.__loggers[logging.ERROR].error(message) def warning(self, message):
message = self.getLogMessage("warning", message) self.__loggers[logging.WARNING].warning(message) def debug(self, message):
message = self.getLogMessage("debug", message) self.__loggers[logging.DEBUG].debug(message) def critical(self, message):
message = self.getLogMessage("critical", message) self.__loggers[logging.CRITICAL].critical(message) if __name__ == "__main__":
logger = TNLog() logger.debug("debug")
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")

Python--logging模块不同级别写入到不同文件的更多相关文章

  1. python logging模块使用

    近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...

  2. (转)python logging模块

    python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...

  3. python logging—模块

    python logging模块 python logging提供了标准的日志接口,python logging日志分为5个等级: debug(), info(), warning(), error( ...

  4. python logging模块使用总结

    目录 logging模块 日志级别 logging.basicConfig()函数中的具体参数含义 format参数用到的格式化信息 使用logging打印日志到标准输出 使用logging.base ...

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

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

  6. 0x01 Python logging模块

    目录 Python logging 模块 前言 logging模块提供的特性 logging模块的设计过程 logger的继承 logger在逻辑上的继承结构 logging.basicConfig( ...

  7. 读懂掌握 Python logging 模块源码 (附带一些 example)

    搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...

  8. Python logging 模块学习

    logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...

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

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

随机推荐

  1. 安卓开发:UI组件-RadioButton和复选框CheckBox

    2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...

  2. MongoDB:配置与安装

    一.配置环境 1.1进行安装下载msi文件 需要注意的一点是,在安装过程中的图示界面不要勾选左下角“安装MongoDB可视化工具”.这是一个客户端管理工具,在后面会具体描述其功能.由于安装时间非常长, ...

  3. MongoDB 3.6版本关于bind_ip设置

    2017年下半年新发布的MongoDB 3.6版本在安全性上做了很大提升,主要归结为两点: 1.将将bind_ip 默认值修改为了localhost: 2. 在db.createUser()和 db. ...

  4. Pycharm使用技巧(转载)

    Pycharm使用技巧(转载) 转载自:http://www.cnblogs.com/cloudtj/articles/5980666.html pycharm使用技巧 https://python. ...

  5. logback日志配置

    第一步:加入jar包.要加入slf4j和logback的jar包,slf4j需要的jar包为slf4j-api,logback需要2个jar包(logback-classic.logback-core ...

  6. Git与SVN交叉使用

    将本地git项目添加到远程svn中 git svn [svnprojpath] svnprojpath为原创svn项目路径 -- 文件夹路径,你要放到哪个文件夹 官方文档中带有-s参数,但我这边加了会 ...

  7. 一个Web项目中实现多个数据库存储数据并相互切换用过吗?

    最近公司一个项目需要连接多个数据库(A和B)操作,根据不同的业务模块查询不同的数据库,因此需要改造下之前的spring-mybatis.xml配置文件以及jdbc.properties配置文件,项目后 ...

  8. css_选择器

    老师的博客:https://www.cnblogs.com/liwenzhou/p/7999532.html 参考w3 school:http://www.w3school.com.cn/css/cs ...

  9. 【English】20190328

    Emotions情绪 [ɪ'moʊʃənz]  Run Your Life for Teens影响你的青少年生活[ti:nz] Don't Let Your Emotions Run Your Lif ...

  10. Java 8 新特性7-方法引用、继承

    (原) 方法引用: 方法引用有4种: 1.静态方法引用:类名::静态方法名 在java中,对集合的排序,我们常用java提供的 Collections.sort(List<T> list, ...