• 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

  • 相关阅读:
    Mac电脑idea中配置nodejs前端环境
    【操作系统】考研真题攻克与重点知识点剖析 - 第 3 篇:内存管理
    设计模式-责任链模式
    VS2015模块库交接出现环境报错 error MSB8031 和 error C1189
    java开发工具IDEA JVM框架教程:Google App Engine配置
    石化能源行业工业互联网智能工厂解决方案
    开启全新教学模式!vLive虚拟直播如何赋能线上教培
    flinksql读oracle写入mysql
    NetCore框架WTM的分表分库实现
    计算机网络-物理层
  • 原文地址:https://blog.csdn.net/davice_li/article/details/127897083