将不同级别的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. C++二分查找算法演示源码

    如下内容段是关于C++二分查找算法演示的内容. #include <cstdio>{ int l = 0, r = n-1; int mid; while (l <= r){ mid ...

  2. ArcFace虹软与Dlib人脸识别对比

    我司最近要做和人脸识别相关的产品,原来使用的是其他的在线平台,识别率和识别速度很满意,但是随着量起来的话,成本也是越来越不能接受(目前该功能我们是免费给用户使用的),而且一旦我们的设备掉线了就无法使用 ...

  3. DVWA 黑客攻防演练(七)Weak Session IDs

    用户访问服务器的时候,一般服务器都会分配一个身份证 session id 给用户,用于标识.用户拿到 session id 后就会保存到 cookies 上,之后只要拿着 cookies 再访问服务器 ...

  4. Docker 创建 Confluence6.12.2 中文版

    目录 目录 1.介绍 1.1.什么是Confluence? 2.Confluence的官网在哪里? 3.如何下载安装? 4.对 Confluence 进行配置 4.1.设置 Confluence 4. ...

  5. ORA-12537: Network Session: End of file

    最近开发组同事使用Azure的Function App访问公司内部的Oracle数据库时,偶尔会遇到"ORA-12537: Network Session: End of file" ...

  6. SQLServer查询计划

    参考:http://blog.csdn.net/luoyanqing119/article/details/17022649 1. 开启方式 菜单栏:query---Display Estimated ...

  7. LeetCode算法题-Range Addition II(Java实现)

    这是悦乐书的第271次更新,第285篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第138题(顺位题号是598).给定一个m行n列的新二维数组M,其初始值为0.提供一个二 ...

  8. HTML之间互相传参

    如图所示,在index.html详情展示中给detailsPanel穿参数,在detailsPanel中获取到参数写ajax到后台获取json数据,那么如何在detailsPanel.html中获取传 ...

  9. web框架开发-Ajax

    Ajax简介 向服务器发送请求的4种方式 1.浏览器地址栏,默认get请求2.form表单: get请求 post请求3.a标签,默认get请求 4.Ajax 特点: 1 异步请求 2 局部刷新 方式 ...

  10. jconsole连接本地进程报安全连接失败

    连接本地程序报错 在idea工具中添加如下命令 -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=888 ...