编程语言
首页 > 编程语言> > python loger 模板

python loger 模板

作者:互联网

一、python loger 模板

def init_logger(app_flask):
    log_conf_dir = {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "standard": {
                # 其中name为getlogger指定的名字
                "format": "[%(asctime)s][%(threadName)s:%(thread)d][%(name)s][%(filename)s:%(lineno)d][%(levelname)s] %(message)s"
            }
        },
        "filters": {},
        "handlers": {
            # 打印到终端的日志
            "console": {
                "level": "DEBUG",
                # 打印到屏幕
                "class": "logging.StreamHandler",
                "formatter": "standard",
            },
            # 打印到文件的日志,收集info及以上的日志
            "default": {
                "level": "INFO",
                # 保存到文件
                "class": "logging.handlers.RotatingFileHandler",
                "formatter": "standard",
                "filename": LOG_FILE_NAME,
                # 日志大小 5M
                "maxBytes": 1024 * 1024 * 5,
                "backupCount": 5,
                "encoding": "utf-8",
            },
            # 打印到文件的日志:收集错误及以上的日志
            "error": {
                "level": "ERROR",
                "class": "logging.handlers.RotatingFileHandler",
                "filename": LOG_ERROR_FILE_NAME,
                "maxBytes": 1024 * 1024 * 5,
                "backupCount": 5,
                "formatter": "standard",
                "encoding": "utf-8",
            },
        },
        "loggers": {
            # logging.getLogger(__name__)拿到的logger配置
            app_name: {
                "handlers": [
                    "default",
                    "console",
                    "error",
                ],
                "level": LOG_LEVEL,
                # True 向上(更高level的logger)传递
                "propagate": False,
            },
            # werkzeug 底层的日志
            "werkzeug": {
                "handlers": [
                    "default",
                    "console",
                    "error",
                ],
                "level": "ERROR",
                "propagate": False,
            },
        },
    }
    dictConfig(log_conf_dir)
``def init_logger(app_flask): log_conf_dir = { "version": 1, "disable_existing_loggers": False, "formatters": { "standard": { # 其中name为getlogger指定的名字 "format": "[%(asctime)s][%(threadName)s:%(thread)d][%(name)s][%(filename)s:%(lineno)d][%(levelname)s] %(message)s" } }, "filters": {}, "handlers": { # 打印到终端的日志 "console": { "level": "DEBUG", # 打印到屏幕 "class": "logging.StreamHandler", "formatter": "standard", }, # 打印到文件的日志,收集info及以上的日志 "default": { "level": "INFO", # 保存到文件 "class": "logging.handlers.RotatingFileHandler", "formatter": "standard", "filename": LOG_FILE_NAME, # 日志大小 5M "maxBytes": 1024 * 1024 * 5, "backupCount": 5, "encoding": "utf-8", }, # 打印到文件的日志:收集错误及以上的日志 "error": { "level": "ERROR", "class": "logging.handlers.RotatingFileHandler", "filename": LOG_ERROR_FILE_NAME, "maxBytes": 1024 * 1024 * 5, "backupCount": 5, "formatter": "standard", "encoding": "utf-8", }, }, "loggers": { # logging.getLogger(__name__)拿到的logger配置 app_name: { "handlers": [ "default", "console", "error", ], "level": LOG_LEVEL, # True 向上(更高level的logger)传递 "propagate": False, }, # werkzeug 底层的日志 "werkzeug": { "handlers": [ "default", "console", "error", ], "level": "ERROR", "propagate": False, }, }, } dictConfig(log_conf_dir)

标签:1024,logging,handlers,python,level,standard,loger,日志,模板
来源: https://www.cnblogs.com/Klay/p/15760239.html