zoukankan      html  css  js  c++  java
  • loggin

    
    # 参考:https://www.cnblogs.com/DI-DIAO/p/8793136.html
    import logging
    
    
    def get_logger():
        logger = logging.getLogger(__name__)
        fh = logging.FileHandler('./test.log')
        # 再创建一个 handler,用于输出到控制台
        ch = logging.StreamHandler()
    
        # 创建一个 formatter,两个 handler 使用相同的日志格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
        # 绑定 formatter 到 handler 上
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
    
        # 绑定 handler 到 logger对象 上
        logger.addHandler(fh)  # logger对象可以添加多个fh和ch对象
        logger.addHandler(ch)
        logger.setLevel(logging.INFO)
        return logger
    
    
    
    #          
    BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
    LOGGING = {
        'version': 1,  # 保留字
        'disable_existing_loggers': False,  # 禁用已经存在的logger实例
        # 日志文件的格式
        'formatters': {
            # 详细的日志格式
            'standard': {
                'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                          '[%(levelname)s][%(message)s]'
            },
            # 简单的日志格式
            'simple': {
                'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
            },
            # 定义一个特殊的日志格式
            'collect': {
                'format': '%(message)s'
            }
        },
        # 过滤器
        'filters': {
            'require_debug_true': {
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        # 处理器
        'handlers': {
            # 在终端打印
            'console': {
                'level': 'DEBUG',
                'filters': ['require_debug_true'],  # 只有在Django debug为True时才在屏幕打印日志
                'class': 'logging.StreamHandler',  #
                'formatter': 'simple'
            },
            # 默认的
            'default': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 3,  # 最多备份几个
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # 专门用来记错误日志
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_err.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # 专门定义一个收集特定信息的日志
            'collect': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_collect.log"),
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'collect',
                'encoding': "utf-8"
            }
        },
        'loggers': {
           # 默认的logger应用如下配置
            '': {
                'handlers': ['default', 'console', 'error'],  # 上线之后可以把'console'移除
                'level': 'DEBUG',
                'propagate': True,  # 向不向更高级别的logger传递
            },
            # 名为 'collect'的logger还单独处理
            'collect': {
                'handlers': ['console', 'collect'],
                'level': 'INFO',
            }
        },
    }
    ######################
    
    log_config = {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "simple": {
                "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
            },
            "standard": {
                "format": "[%(asctime)s][P:%(process)s][T:%(thread)s][%(filename)s[%(funcName)s]: %(lineno)d][%(levelname)s]: %(message)s"
            },
        },
        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "standard"
            },
            "info_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "costIncoming.log"),
                "backupCount": 30,
                "maxBytes": 100*1204*1024,
                "encoding": "utf8"
            },
            "error_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "ERROR",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "errors.log"),
                "maxBytes": 100*1204*1024,
                "backupCount": 30,
                "encoding": "utf8"
            },
            "specified_file_handler": {
                # "class": "logging.handlers.TimedRotatingFileHandler",
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "costIncoming.specified.log"),
                "backupCount": 60,
                # "when": "midnight",
                "maxBytes": 100 * 1204 * 1024,
                "encoding": "utf8"
            },
            "cdr_file_handler": {
                # "class": "logging.handlers.TimedRotatingFileHandler",
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "costIncoming.cdr.log"),
                "backupCount": 60,
                # "when": "midnight",
                "maxBytes": 100 * 1204 * 1024,
                "encoding": "utf8"
            },
            "console_file_handler": {
                # "class": "logging.handlers.TimedRotatingFileHandler",
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "costIncoming.console.log"),
                "backupCount": 60,
                # "when": "midnight",
                "maxBytes": 100 * 1204 * 1024,
                "encoding": "utf8"
            },
            "task_file": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "costIncoming.task.log"),
                "backupCount": 30,
                "maxBytes": 100 * 1204 * 1024,
                "encoding": "utf8"
            },
            "calc_handler": {
                # "class": "logging.handlers.TimedRotatingFileHandler",
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "standard",
                "filename": os.path.join(logs_dir, "cost.calc.log"),
                "backupCount": 60,
                # "when": "midnight",
                "encoding": "utf8"
            },
        },
        "loggers": {
            "flask.app": {
                "level": "INFO",
                "handlers": ["console", "info_file_handler"]
            },
            "cost.cdr": {
                "level": "INFO",
                "handlers": ["console", "cdr_file_handler"]
            },
            "cost.specified": {
                "level": "INFO",
                "handlers": ["console", "specified_file_handler"]
            },
            "cost.console": {
                "level": "INFO",
                "handlers": ["console", "console_file_handler"]
            },
            "task": {
                "level": "INFO",
                "handlers": ["console", "task_file"]
            },
            "cost.calc": {
                "level": "INFO",
                "handlers": ["console", "calc_handler"]
            },
            # "msg_error": {
            #     "level": "ERROR",
            #     "handlers": ["info_file_handler", "error_file_handler"]
            # }
        },
        # 'root': {
        #     'level': 'INFO',
        #     'handlers': ['console', "info_file_handler"]
        # }
    }
    
    ########### 
    from settings.log import log_config
    import logging.config
    logging.config.dictConfig(log_config)
    
    logging.getLogger("collect").error('test')
    
  • 相关阅读:
    LeetCode Find Duplicate File in System
    LeetCode 681. Next Closest Time
    LeetCode 678. Valid Parenthesis String
    LeetCode 616. Add Bold Tag in String
    LeetCode 639. Decode Ways II
    LeetCode 536. Construct Binary Tree from String
    LeetCode 539. Minimum Time Difference
    LeetCode 635. Design Log Storage System
    LeetCode Split Concatenated Strings
    LeetCode 696. Count Binary Substrings
  • 原文地址:https://www.cnblogs.com/lajiao/p/9755648.html
Copyright © 2011-2022 走看看