• logging的小封装可以参考一下


     
    
    1. import logging
    2. import os
    3. from Common.handle_config import conf
    4. from Common.handle_path import logs_dir
    5. class MyLogger(logging.Logger):
    6. def __init__(self,file=None):
    7. # 设置输出级别、输出渠道、输出日志格式
    8. # super().__init__(name,level)
    9. super().__init__(conf.get("log","name"),conf.get("log","level"))
    10. # 日志格式
    11. fmt = '%(asctime)s %(name)s %(levelname)s %(filename)s-%(lineno)d line:%(message)s'
    12. formatter = logging.Formatter(fmt)
    13. # 控制台渠道
    14. handle1 = logging.StreamHandler()
    15. handle1.setFormatter(formatter)
    16. self.addHandler(handle1)
    17. if file:
    18. # 文件渠道
    19. handle2 = logging.FileHandler(file,encoding="utf-8")
    20. handle2.setFormatter(formatter)
    21. self.addHandler(handle2)
    22. # 是否需要写入文件
    23. if conf.getboolean("log","file_ok"):
    24. file_name = os.path.join(logs_dir,conf.get("log","file_name"))
    25. else:
    26. file_name = None
    27. logger = MyLogger(file_name)
    28. logger.info("1111111111111111")
    %(name)s            Name of the logger (logging channel)
    %(levelno)s         Numeric logging level for the message (DEBUG, INFO,
                        WARNING, ERROR, CRITICAL)
    %(levelname)s       Text logging level for the message ("DEBUG", "INFO",
                        "WARNING", "ERROR", "CRITICAL")
    %(pathname)s        Full pathname of the source file where the logging
                        call was issued (if available)
    %(filename)s        Filename portion of pathname
    %(module)s          Module (name portion of filename)
    %(lineno)d          Source line number where the logging call was issued
                        (if available)
    %(funcName)s        Function name
    %(created)f         Time when the LogRecord was created (time.time()
                        return value)
    %(asctime)s         Textual time when the LogRecord was created
    %(msecs)d           Millisecond portion of the creation time
    %(relativeCreated)d Time in milliseconds when the LogRecord was created,
                        relative to the time the logging module was loaded
                        (typically at application startup time)
    %(thread)d          Thread ID (if available)
    %(threadName)s      Thread name (if available)
    %(process)d         Process ID (if available)
    %(message)s         The result of record.getMessage(), computed just as
                        the record is emitted

    1. import os
    2. from Common.handle_path import conf_dir
    3. class HandleConfig(ConfigParser):
    4. def __init__(self,file_path):
    5. super().__init__()
    6. self.read(file_path, encoding="utf-8")
    7. file_path = os.path.join(conf_dir, "nmb.ini")
    8. conf = HandleConfig(file_path)
    9. # if __name__ == '__main__':
    10. # conf = HandleConfig("nmb.ini")
    11. # conf.get("log","name")

    1. [log]
    2. name = py30
    3. level = INFO
    4. file_ok = True
    5. file_name = py30.log

  • 相关阅读:
    深入理解Java虚拟机读书笔记--11 Java与线程
    【LeetCode】1752. 检查数组是否经排序和轮转得到
    PyTorch语音识别的理论基础——MFCC
    yolact 环境配置
    MQ - 36 云原生:业界MQ的计算存储分离的设计与实现
    1-Redis架构设计到使用场景-四种部署运行模式(上)
    基于TCP/UDP协议的Socket编程方法
    Java代码实现RSA算法加密解密文件功能
    品牌线上打假,应防微杜渐
    王道数据结构编程题 链表
  • 原文地址:https://blog.csdn.net/davice_li/article/details/127897083