Python线上环境如何优雅地使用日志?( 二 )

sub_of_main.py
# -*- coding: utf-8 -*-__auth__ = 'zone'__date__ = '2019/6/17 下午11:47''''公众号:zone7小程序:编程面试题库'''import loggingmodule_logger = logging.getLogger("zone7Model.sub.module")class SubOfMain(object): def __init__(self): self.logger = logging.getLogger("zone7Model.sub.module") self.logger.info("init sub class") def print_some_log(self): self.logger.info("sub class log is printed")def som_function(): module_logger.info("call function some_function")使用配置文件配置 logging
这里分别给出了两种配置文件的使用案例,都分别使用了三种输出,输出到命令行、输出到文件、将错误信息独立输出到一个文件
log_cfg.json
{ "version":1, "disable_existing_loggers":false, "formatters":{ "simple":{ "format":"%(asctime)s - %(name)s - %(levelname)s - %(message)s" } }, "handlers":{ "console":{ "class":"logging.StreamHandler", "level":"DEBUG", "formatter":"simple", "stream":"ext://sys.stdout" }, "info_file_handler":{ "class":"logging.handlers.RotatingFileHandler", "level":"INFO", "formatter":"simple", "filename":"info.log", "maxBytes":10485760, "backupCount":20, "encoding":"utf8" }, "error_file_handler":{ "class":"logging.handlers.RotatingFileHandler", "level":"ERROR", "formatter":"simple", "filename":"errors.log", "maxBytes":10485760, "backupCount":20, "encoding":"utf8" } }, "loggers":{ "my_module":{ "level":"ERROR", "handlers":["info_file_handler2"], "propagate":"no" } }, "root":{ "level":"INFO", "handlers":["console","info_file_handler","error_file_handler"] }}通过 json 文件读取配置:
import jsonimport logging.configimport osdef set_log_cfg(default_path="log_cfg.json", default_level=logging.INFO, env_key="LOG_CFG"): path = default_path value = https://www.isolves.com/it/cxkf/yy/Python/2021-03-08/os.getenv(env_key, None) if value: path = value if os.path.exists(path): with open(path, "r") as f: config = json.load(f) logging.config.dictConfig(config) else: logging.basicConfig(level=default_level)def record_some_thing(): logging.info("Log level info") logging.debug("Log level debug") logging.warning("Log level warning")if __name__ == "__main__": set_log_cfg(default_path="log_cfg.json") record_some_thing()log_cfg.yaml
version: 1disable_existing_loggers: Falseformatters: simple: format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout info_file_handler: class: logging.handlers.RotatingFileHandler level: INFO formatter: simple filename: info.log maxBytes: 10485760 backupCount: 20 encoding: utf8 error_file_handler: class: logging.handlers.RotatingFileHandler level: ERROR formatter: simple filename: errors.log maxBytes: 10485760 backupCount: 20 encoding: utf8loggers: my_module: level: ERROR handlers: [info_file_handler] propagate: noroot: level: INFO handlers: [console,info_file_handler,error_file_handler]通过 yaml 文件读取配置:
import yamlimport logging.configimport osdef set_log_cfg(default_path="log_cfg.yaml", default_level=logging.INFO, env_key="LOG_CFG"): path = default_path value = https://www.isolves.com/it/cxkf/yy/Python/2021-03-08/os.getenv(env_key, None) if value: path = value if os.path.exists(path): with open(path, "r") as f: config = yaml.load(f) logging.config.dictConfig(config) else: logging.basicConfig(level=default_level)def record_some_thing(): logging.info("Log level info") logging.debug("Log level debug") logging.warning("Log level warning")if __name__ == "__main__": set_log_cfg(default_path="log_cfg.yaml") record_some_thing()
【Python线上环境如何优雅地使用日志?】


推荐阅读