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

# -*- coding: utf-8 -*-
import os
import time
from logging.handlers import RotatingFileHandler
import logging import inspect dir = os.path.dirname(__file__) handlers = {logging.NOTSET: os.path.join(dir, 'notset.log'), logging.DEBUG: os.path.join(dir, 'debug.log'), logging.INFO: os.path.join(dir, 'info.log'), logging.WARNING: os.path.join(dir, 'warning.log'), logging.ERROR: os.path.join(dir, 'error.log'), logging.CRITICAL: os.path.join(dir, 'critical.log'),
} 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")

将不同级别的logging 日志信息写入不同文件的更多相关文章

  1. c#.NET中日志信息写入Windows日志中解决方案

    1. 目的应用系统的开发和维护离不开日志系统,选择一个功能强大的日志系统解决方案是应用系统开发过程中很重要的一部分.在.net环境下的日志系统解决方案有许多种,log4net是其中的佼佼者.在Wind ...

  2. flink---实时项目--day01--1. openrestry的安装 2. 使用nginx+lua将日志数据写入指定文件中 3. 使用flume将本地磁盘中的日志数据采集到的kafka中去

    1. openrestry的安装 OpenResty = Nginx + Lua,是⼀一个增强的Nginx,可以编写lua脚本实现⾮非常灵活的逻辑 (1)安装开发库依赖 yum install -y ...

  3. 转:Windows下WSH/JS实现SVN服务器钩子脚本阻止提交空日志信息和垃圾文件

    http://blog.csdn.net/caikanxp/article/details/8279921 如何强制用户在提交SVN时填写日志信息? 如果用户使用的都是TortoiseSVN客户端,可 ...

  4. Log4j(一):Log4j将日志信息写入数据库

    前言 为了监听一些数据的采集等功能,需要随时监听设备的状态,所以需要运行的时候将日志打入到数据库中. 正文 第一步: 首先是jar包,由于我使用的是springboot,所以,在springboot- ...

  5. 【python】logging日志模块写入中文编码错误解决办法

    一.问题: 使用python的logging模块记录日志,有时会遇到中文编码问题错误. 二.解决办法: 在logging.FileHandler(path) 中添加指定编码方式 encoding='u ...

  6. 【Python语言】--Crontab结合Python脚本实现将日志每天写入到文件中

    一.前述 实际工作中将Python脚本每天定时写入到日志文件中的使用场景还是蛮多的,有很多种方法可以实现这种效果.本文选择一种方式实现,特将实现细节做如下分享,不当之处烦请指正. 二.具体 1.pyt ...

  7. 解决logging模块日志信息重复问题

    解决logging模块日志信息重复问题 问题描述 相信大家都知道python的logging模块记录日志信息的步骤: # coding:utf-8 import logging ### 创建logge ...

  8. python-整理-logging日志

    python的日志功能模块是logging 功能和使用方式非常类似于log4 如何使用logging: # 导入日志模块import logging# 使用配置文件设置日志时,需要导入这个模块 imp ...

  9. ASP.NET Core 实战:使用 NLog 将日志信息记录到 MongoDB

    一.前言 在项目开发中,日志系统是系统的一个重要组成模块,通过在程序中记录运行日志.错误日志,可以让我们对于系统的运行情况做到很好的掌控.同时,收集日志不仅仅可以用于诊断排查错误,由于日志同样也是大量 ...

随机推荐

  1. 【jsPDF】jsPDF插件实现将html页面转换成PDF,并下载,支持分页

    1.目的:在前段是 jQuery库 或者 VUE库 或者两者混合库,将html 页面和数据 转换成PDF格式并下载,支持分页 1.项目背景: 对客户报修记录进行分类统计,并生成各种饼图.柱状图.线性图 ...

  2. 树莓派进阶之路 (009) - 树莓派ftp脚本(原创)

    FTP.sh #!/bin/sh cd echo "彻底卸载原有的ftp" sudo apt-get remove --purge vsftpd #(--purge 选项表示彻底删 ...

  3. Python学习笔记(十)—— 高级特性

    一.切片 1.定义: 经常取指定索引范围的操作,用循环十分繁琐,因此,Python提供了切片(Slice)操作符. 2.语法: A[1:3] 取出1到3,都是正数的情况下,缺填的为0(第一个),end ...

  4. MapReduce 模式、算法和用例

    翻译自:http://highlyscalable.wordpress.com/2012/02/01/mapreduce-patterns/ 在这篇文章里总结了几种网上或者论文中常见的MapReduc ...

  5. [AaronYang原创] 敏捷开发-Jira 6.0.5环境搭建[2]

    基本配置-关卡一(我研究了1.5个小时 AaronYang)   JIRA的设置向导将只显示您安装后第一次JIRA. 一旦你完成了它,你不能再次运行它. 然而,每一个设置在设置向导配置可以通过管理控制 ...

  6. poj3041(最小顶点覆盖)

    链接:点击打开链接 题意:N*N的矩阵中有一些点代表陨石.每次仅仅能消灭一行或一列连,问须要多少次才干所有消灭 代码: #include <map> #include <queue& ...

  7. PowerShell 获取Site Collection下被签出的文件

    由于权限的设置,当文件被签出时导致别人不可见了,这对校验文件个数的人来说着实是件烦恼的事.幸好利用PowerShell,可以获取Site Collection下被签出的文件. Resolution A ...

  8. SAML

    From the book <Modern Authentication with Azure Active Directory for Web Applications> SAML Th ...

  9. (面试题)html中创建模态窗口的方法有哪些?

    一.创建模态和非模态对话框除了alert(""); confirm(""); prompt("");之外还有创建模态对话框:vReturnV ...

  10. python标准库介绍——29 zlib 模块详解

    ==zlib 模块== (可选) ``zlib`` 模块为 "zlib" 压缩提供支持. (这种压缩方法是 "deflate".) [Example 2-43 ...