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配置日志

logger_file = "%s%s.log" % (LogPath, Setting.box_id)
print "Will Log into %s"%logger_file
logger = logging.getLogger(name) # 获取或初始化logger,不传name,name就是设置的root,默认logging.info/error就是
logger.set...

python日志

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import logging

logging.basicConfig(level=logging.DEBUG,format='%(levelname)s:%(filename)s:%(lineno)d:%(message)s')

logging.debug("test")
logging.info("t...

Python + logging 控制台有日志输出,但日志文件中数据为空

源码:

def output(self, level, message):
fh = logging.FileHandler(self.logpath, mode='a', encoding='utf-8')
fh.setLevel(logging.DEBUG)
fh.setFormatter(self.formatter)
...

文章标题:python loger 模板
文章链接:https://www.dianjilingqu.com/4000.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。
THE END
< <上一篇
下一篇>>