zoukankan      html  css  js  c++  java
  • Python中的logging日志模块

    import os,sys,logging
    from logging import handlers
    
    
    class Loggers(object):
    
        # 日志级别关系映射
        level_relations = {
            'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING,
            'error': logging.ERROR, 'critical': logging.CRITICAL
        }
    
        def __init__(self,filename='test.log',when='D',backCount=30,level='info', log_dir='log',
                     fmt='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
            if not os.path.exists(log_dir):
                os.makedirs(log_dir)
    
            self.logger = logging.getLogger(filename)
            self.log_name = os.path.join(log_dir,filename)
            format_str = logging.Formatter(fmt)  # 设置日志格式
            self.logger.setLevel(self.level_relations.get(level))  # 设置日志级别
            file_handler = handlers.TimedRotatingFileHandler(filename=self.log_name,when=when,backupCount=backCount,encoding='utf-8')
            stream_handler = logging.StreamHandler()  # 往屏幕上输出
            stream_handler.setFormatter(format_str)   # 往屏幕上输出
            file_handler.setFormatter(format_str)
            self.logger.addHandler(file_handler)
            self.logger.addHandler(stream_handler)    # 往屏幕上输出
    

      

  • 相关阅读:
    并发工具类的使用 CountDownLatch,CyclicBarrier,Semaphore,Exchanger
    多线程按顺序执行3个方法
    读写锁事例
    使用AQS自定义重入锁
    java 几种锁实现
    Nginx 安装
    Windows 安装mysql
    day--14前端(HTML、CSS)
    day13--开发堡垒机
    day12--python操作mysql
  • 原文地址:https://www.cnblogs.com/lucktomato/p/14962346.html
Copyright © 2011-2022 走看看