Can I have logging.ini file without root logger?

前端 未结 2 1059
长发绾君心
长发绾君心 2021-02-15 15:51

Here is how my logging.ini file looks like:

[loggers]
keys=teja

[handlers]
keys=fileHandler

[formatters]
keys=simpleFormatter

[logger_teja]
level=DEBUG
handl         


        
2条回答
  •  失恋的感觉
    2021-02-15 16:20

    If you use the source, you'll see that you must configure a root logger:

    # configure the root first
    llist = cp["loggers"]["keys"]
    llist = llist.split(",")
    llist = list(map(lambda x: x.strip(), llist))
    llist.remove("root")
    section = cp["logger_root"]
    root = logging.root
    log = root
    

    (where cp is the configparser that loads the .ini file you passed in)

    The only reason I can think of is that explicit is better than implicit, so it forces you to declare exactly what you want to do with the root logger, in case you thought it would do some magic. Though I don't thinkg that's a particularly good reason. It was probably just the way someone thought to do it at the time. If you do some further reading:

    The fileConfig() API is older than the dictConfig() API and does not provide functionality to cover certain aspects of logging [... N]ote that future enhancements to configuration functionality will be added to dictConfig(), so it’s worth considering transitioning to this newer API when it’s convenient to do so.

    And if you consider the dictConfig docs, it appears that you don't have to provide a root logger.

    So it appears that you are required to specify a root handler, with no really good reason besides backwards compatibility. If you want to get around that, you'll have to either specify your settings in a Python file or import a JSON file and use the dictConfig method.

提交回复
热议问题