将不同级别的logging 日志信息写入不同文件
将不同级别的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 日志信息写入不同文件的更多相关文章
- c#.NET中日志信息写入Windows日志中解决方案
1. 目的应用系统的开发和维护离不开日志系统,选择一个功能强大的日志系统解决方案是应用系统开发过程中很重要的一部分.在.net环境下的日志系统解决方案有许多种,log4net是其中的佼佼者.在Wind ...
- flink---实时项目--day01--1. openrestry的安装 2. 使用nginx+lua将日志数据写入指定文件中 3. 使用flume将本地磁盘中的日志数据采集到的kafka中去
1. openrestry的安装 OpenResty = Nginx + Lua,是⼀一个增强的Nginx,可以编写lua脚本实现⾮非常灵活的逻辑 (1)安装开发库依赖 yum install -y ...
- 转:Windows下WSH/JS实现SVN服务器钩子脚本阻止提交空日志信息和垃圾文件
http://blog.csdn.net/caikanxp/article/details/8279921 如何强制用户在提交SVN时填写日志信息? 如果用户使用的都是TortoiseSVN客户端,可 ...
- Log4j(一):Log4j将日志信息写入数据库
前言 为了监听一些数据的采集等功能,需要随时监听设备的状态,所以需要运行的时候将日志打入到数据库中. 正文 第一步: 首先是jar包,由于我使用的是springboot,所以,在springboot- ...
- 【python】logging日志模块写入中文编码错误解决办法
一.问题: 使用python的logging模块记录日志,有时会遇到中文编码问题错误. 二.解决办法: 在logging.FileHandler(path) 中添加指定编码方式 encoding='u ...
- 【Python语言】--Crontab结合Python脚本实现将日志每天写入到文件中
一.前述 实际工作中将Python脚本每天定时写入到日志文件中的使用场景还是蛮多的,有很多种方法可以实现这种效果.本文选择一种方式实现,特将实现细节做如下分享,不当之处烦请指正. 二.具体 1.pyt ...
- 解决logging模块日志信息重复问题
解决logging模块日志信息重复问题 问题描述 相信大家都知道python的logging模块记录日志信息的步骤: # coding:utf-8 import logging ### 创建logge ...
- python-整理-logging日志
python的日志功能模块是logging 功能和使用方式非常类似于log4 如何使用logging: # 导入日志模块import logging# 使用配置文件设置日志时,需要导入这个模块 imp ...
- ASP.NET Core 实战:使用 NLog 将日志信息记录到 MongoDB
一.前言 在项目开发中,日志系统是系统的一个重要组成模块,通过在程序中记录运行日志.错误日志,可以让我们对于系统的运行情况做到很好的掌控.同时,收集日志不仅仅可以用于诊断排查错误,由于日志同样也是大量 ...
随机推荐
- 信息列表中的ContentObserver、CONTENT_URI等
1. 注册ContentObserver时Sms.Inbox.CONTENT_URI应改成Sms.CONTENT_URI. getContentResolver().registerContentOb ...
- ubuntu下安装迅雷thunder
迅雷是windows xp下必装的下载工具,作为一款跨协议的下载软件,迅雷的下载速度极其强悍. 那么在ubuntu下能否安装迅雷呢? 到ubuntu中文论坛逛了一圈,发现有现成的wine-thunde ...
- [转]图解分布式一致性协议Paxos
Paxos协议/算法是分布式系统中比较重要的协议,它有多重要呢? <分布式系统的事务处理>: Google Chubby的作者MikeBurrows说过这个世界上只有一种一致性算法,那就是 ...
- Ubuntu下查看软件版本及安装位置【转】
Ubuntu下查看软件版本及安装位置 查看软件版本: aptitude show xxx 也可用apt-show-versions (要先安装sudo apt-get install apt- ...
- Java使用Itext5.5.10进行pdf签章
说到PDF数字签名签章,这个其实也是数字证书信息安全的应用范畴,关于数字证书和数字签名,网上有很多解释说明,但讲解都多不够详细准确,这边推荐一篇大神的博文,讲解浅显易懂形象数字证书 数字签名 数据加密 ...
- Oracle 12C -- truncate的级联操作
在之前的版本中,存在外键约束时,无法直接truncate父表.在12C中,对truncate操作添加了级联操作特性. 前提是创建外键约束时,使用了"on delete casacde&quo ...
- Linux中iptables防火墙指定端口范围
我需要700至800之间的端口都能tcp访问 代码如下 复制代码 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 7 ...
- Python 文件 writelines() 方法
概述 Python 文件 writelines() 方法用于向文件中写入一序列的字符串. 这一序列字符串可以是由迭代对象产生的,如一个字符串列表. 换行需要制定换行符 \n. 语法 writeline ...
- 【转载并整理】ORACLE锁机制
转载文章:http://blog.csdn.net/liuyiy/article/details/25005393 转载文章:http://www.cnblogs.com/jiyuqi/p/37017 ...
- 转 阿里Dubbo疯狂更新,关Spring Cloud什么事?
原文地址: http://www.ityouknow.com/springcloud/2017/11/20/dubbo-update-again.html阿里Dubbo疯狂更新,关Spring Clo ...