Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...