python日志记录 配置 yml

https://docs.python.org/zh-cn/3/howto/logging.html#logging-advanced-tutorial

解释器用于标准输入、标准输出和标准错误的 文件对象:

  • stdin 用于所有交互式输入(包括对 input() 的调用);

  • stdout 用于 print() 和 expression 语句的输出,以及用于 input() 的提示符;

  • 解释器自身的提示符和它的错误消息都发往 stderr

test.yml

version: 1

formatters:
  simpleFormatter:
    format: '[%(asctime)s] %(name)-25s [%(levelname)-6s]: %(message)s'

handlers:

  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simpleFormatter
    stream: ext://sys.stdout

  eastmoneyHandlers:
    class : logging.handlers.RotatingFileHandler
    formatter: simpleFormatter
    filename: ...\log\eastmoney.log
    maxBytes: 1024000
    encoding: utf-8
    backupCount: 5

loggers:

  simpleExample:
    level: DEBUG
    handlers: [console]
    propagate: no

  others.eastmoney:
    level: DEBUG
    handlers: [eastmoneyHandlers, console]
    propagate: no

root:
  level: WARNING
  handlers: [console]

demo.py

import logging
from logging import config
import yaml

with open(r"./test.yml", 'r', encoding="utf-8") as f:
    logging_yaml = yaml.load(stream=f, Loader=yaml.FullLoader)
    # 配置logging日志:主要从文件中读取handler的配置、formatter(格式化日志样式)、logger记录器的配置
    logging.config.dictConfig(config=logging_yaml)
# 获取根记录器:配置信息从yaml文件中获取
log = logging.getLogger("others.eastmoney")
log.info("INFO输出")
log.error('ERROR输出')
log.debug("DEBUG输出")

你可能感兴趣的:(python日志记录 配置 yml)