技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

再次改进日志类 --!

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
改进的日志类:
1. 使用format对象,就不用自己采集环境信息,库可以采集信息
2. 将多个handler绑定到一个logger上,且每个handler设置相应级别
   日志会产生多份,每个handler只记录自己的级别和含高于自己级别的日志
3. 还是需要inspect库采集环境信息,因为logging的API被封装过了,
   format只能采集直接调用logging模块API的函数信息
'''

import os
import sys
import time
import logging
import inspect

handlers = {logging.NOTSET:"/tmp/TNLOG-notset.log",
            logging.INFO:"/tmp/TNLOG-info.log",
            logging.DEBUG:"/tmp/TNLOG-debug.log",
            logging.WARNING:"/tmp/TNLOG-warning.log",
            logging.ERROR:"/tmp/TNLOG-error.log",
            logging.CRITICAL:"/tmp/TNLOG-critical.log"}

def createHandlers():
    logLevels = handlers.keys()
    '''日志格式:[时间] [类型] [记录代码] 信息'''
    fmt = '[%(asctime)s] [%(levelname)s] %(message)s '
    formatter = logging.Formatter(fmt)
    for level in logLevels:
        path = os.path.abspath(handlers[level])
        print path
        handlers[level] = logging.FileHandler(path)
        handlers[level].setLevel(level)
        handlers[level].setFormatter(formatter) 

#加载模块时创建全局变量
createHandlers()

class TNLog(object):
    '''TNLog.constant'''
    NOTSET = logging.NOTSET
    INFO = logging.INFO
    DEBUG = logging.DEBUG
    WARNING = logging.WARNING
    ERROR = logging.ERROR
    CRITICAL = logging.CRITICAL
    
    def __init__(self,setLevel=logging.NOTSET):
        self.__loggers = logging.getLogger()
        logLevels = handlers.keys()
        for level in logLevels:
            self.__loggers.addHandler(handlers[level]) #将多个handler绑到一个logger上
        self.__loggers.setLevel(setLevel)
        
    def addCallingPointInfo(self,message):
        frame,filename,lineNo,functionName,code,iUnknowField = inspect.stack()[2]
        '''[文件名 - 行数 - 函数名] 信息'''
        return "[%s - %s - %s] %s" %(filename,lineNo,functionName,message)
    
    def info(self,message):
        message = self.addCallingPointInfo(message)
        self.__loggers.info(message)
    
    def error(self,message):
        message = self.addCallingPointInfo(message)
        self.__loggers.error(message)
    
    def warning(self,message):
        message = self.addCallingPointInfo(message)
        self.__loggers.warning(message)
    
    def debug(self,message):
        message = self.addCallingPointInfo(message)
        self.__loggers.debug(message)
    
    def critical(self,message):
        message = self.addCallingPointInfo(message)
        self.__loggers.critical(message)
        
#全局logger
logger = TNLog(TNLog.DEBUG)
def getLogger():
    return logger

if __name__ == "__main__":
    logger = getLogger()
    logger.debug("debug")
    logger = getLogger()
    logger.info("info")
    logger = getLogger()
    logger.warning("warning")
    logger = getLogger()
    logger.error("error")
    logger = getLogger()
    logger.critical("critical")
复制代码

posted on   codestyle  阅读(226)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示