Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python.logging: Why does my non-basicConfig setting not work?

I want to log to a single log file from main and all sub modules.

The log messages send from a main file, where I define the logger, work as expected. But the ones send from a call to an imported function are missing.

It is working if I use logging.basicConfig as in Example 1 below. But the second example which allows for more custom settings does not work.

Any ideas why?

# in the submodule I have this code
import logging
logger = logging.getLogger(__name__)

EXAMPLE 1 - Working

Here I create two handlers and just pass them to basicConfig:

# definition of root looger in main module

formatter = logging.Formatter(fmt="%(asctime)s %(name)s.%(levelname)s: %(message)s", datefmt="%Y.%m.%d %H:%M:%S")   

handler = logging.FileHandler('logger.log')
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)

handler2 = logging.StreamHandler(stream=None)
handler2.setFormatter(formatter)
handler2.setLevel(logging.DEBUG)

logging.basicConfig(handlers=[handler, handler2], level=logging.DEBUG)
logger = logging.getLogger(__name__)

EXAMPLE 2 - Not working

Here I create two handlers and addHandler() them to the root logger:

# definition of root looger in main module

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

handler = logging.FileHandler('logger.log')
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
#handler.setLevel(logging.ERROR)
logger.addHandler(handler)

handler = logging.StreamHandler(stream=None)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
like image 896
Joe Avatar asked May 09 '26 20:05

Joe


1 Answers

You need to configure the (one and only) root logger in the main module of your software. This is done by calling

logger = logging.getLogger() #without arguments

instead of

logger = logging.getLogger(__name__)

(Python doc on logging)

The second example creates a separate, child logger with the name of your script.

If there are no handlers defined in the submodules, the log message is being passed down to the root logger to handle it.

A related question can be found here: Python Logging - How to inherit root logger level & handler

like image 191
jedwards Avatar answered May 11 '26 10:05

jedwards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!