我在文件 loggingConfig.yml 中有以下记录器配置
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
file_handler:
class: logging.FileHandler
level: INFO
formatter: simple
filename: info.log
encoding: utf8
loggers:
my_module:
level: ERROR
handlers: [console]
propagate: no
root:
level: INFO
handlers: [console, file_handler]
Run Code Online (Sandbox Code Playgroud)
以及以下python代码:
import logging
import logging.config
import yaml
with open('loggingConfig.yml', 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
logger.info('TestLogger')
Run Code Online (Sandbox Code Playgroud)
这工作正常,但现在我想以写入模式而不是追加模式打开日志文件。我找不到任何使用 yaml 文件并以写入模式打开日志文件的示例。
我只发现,可以使用 fileConfig 以写模式打开
logging.config.fileConfig('logging.conf')
Run Code Online (Sandbox Code Playgroud)
并在 …