将不同级别的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. 【学习笔记】【Design idea】一、Java异常的设计思想、性能相关、笔记

    1.前言: 异常.本该是多么优雅的东西,然而,得全靠自己在零散的信息中汇集. 学习笔记保持更新. 2.教材(参考资料) 其他 ①受检异常与非受检异常:https://www.cnblogs.com/j ...

  2. Dubbo开发,利用项目模拟提供者和消费者之间的调用--初学

    开发工具:IDEA,虚拟机 VMware Workstation 预备工作:安装好zookeeper的虚拟机,电脑jdk更换为1.7,本地tomcat启动,能够访问以下页面即可进行开发 2.建立以下s ...

  3. 关于 MongoDB 与 SQL Server 通过本身自带工具实现数据快速迁移 及 注意事项 的探究

    背景介绍 随着业务的发展.需求的变化,促使我们追求使用不同类型的数据库,充分发挥其各自特性.如果决定采用新类型的数据库,就需要将既有的数据迁移到新的数据库中.在这类需求中,将SQL Server中的数 ...

  4. linux 大小写转化

    (1)sed: cat file | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' (2)tr: cat file | ...

  5. SQLServer\framework启动报异常:Module的类型初始值设定项引发异常

    net framework卸载 重装 https://download.microsoft.com/download/E/4/1/E4173890-A24A-4936-9FC9-AF930FE3FA4 ...

  6. springboot运行时该注意的地方

    1.SpringBoot的入口类一定要放在所有类的上一层,也就是要包含所有子包中的类 springBoot在启动时才会加载所有子包中的所有组件 否则会出现启动成功但是访问不到的情况: This app ...

  7. oc中的委托模式

    通过一个例子来理解委托模式 首先定义个协议 协议(protocol) :它可以声明一些必须实现的方法和选择实现的方法  (在java中称为接口) // // StudentDelegate.h // ...

  8. 多线程中的event,用于多线程的协调

    ''' 简单的需求:红绿灯,红灯停,绿灯行 一个线程扮演红绿灯,每过一段时间灯变化,3-5个线程扮演车,红灯停,绿灯行 红绿灯线程和车的线程会相互依赖 这种场景怎么实现?---事件 切换一次灯就是一次 ...

  9. 【Python使用】使用pip安装卸载Python包(含离线安装Python包)未完成???

    pip 是 Python 包管理工具,该工具提供了对Python包的查找.下载.安装.卸载的功能.Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具. pip使用( ...

  10. supervisor management kafka zookeeper

    # cat kafka.ini [program:kafka] command=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/ ...