Registering and inheriting registrar configurations in Python

I came from SLF4J and Log4J, so this may be the reason that I do not understand how the log works in Python.

I have the following

---- logging.yaml

version: 1

handlers:

  console:
    class: logging.StreamHandler
    level: DEBUG
    stream: ext://sys.stderr
    formatter: simpleFormatter

  file:
    class: logging.FileHandler
    filename: app.log
    mode: w
    level: DEBUG
    formatter: simpleFormatter

formatters:
  simpleFormatter:
    #class: !!python/name:logging.Formatter
    #class: logging.Formatter
    format: '%(name)s %(asctime)s %(levelname)s %(message)s'
    datefmt: '%d/%m/%Y %H:%M:%S'


root:
  level: INFO
  handlers: [console, file]

mod:
  level: DEBUG

----- mod.py

import logging

def foo ():
    log = logging.getLogger ( __name__ )
    log.debug ( 'Hello from the module' )

---- main.py

from logging.config import dictConfig
import yaml
with open ( 'logging.yaml' ) as flog:
    dictConfig ( yaml.load ( flog ) )

import logging

from mod import foo

if __name__ == '__main__':

    log = logging.getLogger ( __name__ )
    log.debug ( 'Hello from main' )

    foo ()

With the above configuration, I expect to see only the message 'Hello from the module'. Instead, nothing is printed. When I install DEBUGfor the root logger, both messages are printed.

So, aren't messages redirected to the upper registrars? Is the journal a modchild root? Does logger write the modconfiguration handlers? (I tried to repeat handlersin mod, but nothing changes).

, : INFO, DEBUG, , root?

+4
1

: , , root, loggers :

a dict, , dict , Logger

, :

loggers:
  mod:
    level: DEBUG

:

$ python main.py
mod 20/07/2016 14:35:32 DEBUG Hello from the module

$ cat app.log
mod 20/07/2016 14:35:32 DEBUG Hello from the module
+3

Source: https://habr.com/ru/post/1648500/


All Articles