Logging is sometimes duplicated without color #118
Replies: 5 comments
-
It's extremly likely that somewhere in the code another handler is being created. Maybe |
Beta Was this translation helpful? Give feedback.
-
You might be right, although I'm struggling to figure out where. I'll try to raise an error if a handler already exists, and see what the traceback says! |
Beta Was this translation helpful? Give feedback.
-
This might be related. I notice that if I erroneously call import colorlog
import logging
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
'%(log_color)s%(levelname)s:%(name)s:%(message)s'))
logger = colorlog.getLogger('example')
logger.addHandler(handler)
logger.warning('Normal logging')
logging.warning("Logging using root") # using logging instead of logger
logger.warning('This message is printed twice') # logger now outputs message twice This also applies to the normal logger: import logging
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(
'%(levelname)s:%(name)s:%(message)s'))
logger = logging.getLogger('example')
logger.addHandler(handler)
logger.warning('Normal logging')
# WARNING:example:Normal logging
logging.warning("Logging using root") # using logging instead of logger
# WARNING:root:Logging using root
logger.warning('This message is printed twice') # logger now outputs message twice
# WARNING:example:This message is printed twice
# WARNING:example:This message is printed twice Now, I don't call logging.debug by accident (I mean, I did and that is how I discovered this, but that is no longer the case), but the above does look identical to my behaviour, except that in my case there is never output a root logging message. |
Beta Was this translation helpful? Give feedback.
-
After calling the above, there is still only one handler in |
Beta Was this translation helpful? Give feedback.
-
https://github.com/python/cpython/blob/3.10/Lib/logging/__init__.py#L2137-L2139 In your example you'll probably find that |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Asking here primarily to see if you know an obvious reason why this is happening:
I'm sometimes getting a double logging output in my log:
The white lines containing :main: are the lines I do not expect. Once these lines start appearing, the double logs appear with every single logging statement.
We import a logger in our scripts that is defined as:
I've use this as
logger = make_logger()
, and then e.g.logger.debug(...)
. I've noticed that I can trigger the double line behaviour if I accidentally calllogging.debug
instead oflogger.debug
, but that is not the case in the above.Do you have any idea what's going on?
Beta Was this translation helpful? Give feedback.
All reactions