3
我有以下工作正常,如果我使用basicConfig()在Naming Python loggers瞭解Python的記錄器名稱
一切所描述的實踐命名我的Python記錄器。但現在我試圖使用配置文件和dictConfig()在運行時配置記錄器。
http://docs.python.org/2/library/logging.config.html#dictionary-schema-details的文檔似乎說我可以在配置根記錄器的字典中有一個「根」鍵。但是如果我只配置這個記錄器,我沒有得到任何輸出。
這是我有:
logging_config.yaml
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(pathname)s:%(lineno)s - %(message)s'
datefmt: '%Y%m%d %H:%M:%S'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
file:
class: logging.FileHandler
level: DEBUG
formatter: simple
filename: 'test.log'
mode: "w"
# If I explicitly define a logger for __main__, it works
#loggers:
# __main__:
# level: DEBUG
# handlers: [console, file]
root:
level: DEBUG
handlers: [console, file]
test_log.py
import logging
logger = logging.getLogger(__name__)
import logging.config
import yaml
if __name__ == "__main__":
log_config = yaml.load(open("logging_config.yaml", "r"))
logging.config.dictConfig(log_config)
#logging.basicConfig() #This works, but dictConfig doesn't
logger.critical("OH HAI")
logging.shutdown()
爲什麼不這樣產生的任何日誌輸出,這有什麼解決的正確方法它?