python loger 模板

2022/1/3 20:12:10

本文主要是介绍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)

这篇关于python loger 模板的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程