python日志記錄 配置 yml

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

解釋器用于標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤的 文件對象

  • stdin 用于所有交互式輸入(包括對 input() 的調(diào)用);

  • stdout 用于 print()expression 語句的輸出,以及用于 input() 的提示符;

  • 解釋器自身的提示符和它的錯誤消息都發(fā)往 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輸出")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容