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輸出")