message being shown twice
I'm trying to customize the messages for each logger level.
For example, for a logger.info I want to use the following format:
"{time:YYYY-MM-DD HH:mm:ss.SSS} |
For a logger.debug:
"{time:YYYY-MM-DD HH:mm:ss.SSS} |
I'm using the following code:
from loguru import logger
import sys
DEBUG_FORMAT = "{time:YYYY-MM-DD HH:mm:ss.SSS} | <level>{level: <8}</level> | {name}:{function}:{line} - <level>{message}</level>"
INFO_FORMAT = "{time:YYYY-MM-DD HH:mm:ss.SSS} | <level>{level: <8}</level> | <level>{message}</level>"
logger.remove()
logger.add(sink=sys.stderr, level="INFO", format=INFO_FORMAT)
logger.add(sink=sys.stderr, level="DEBUG", format=DEBUG_FORMAT)
logger.info("my log test using INFO....")
logger.debug("this is a DEBUG log....")
but this code is showing:
2024-06-30 20:32:38.863 | INFO | my log test using INFO....
2024-06-30 20:32:38.863 | INFO | __main__:<module>:11 - my log test using INFO....
2024-06-30 20:32:38.864 | DEBUG | __main__:<module>:12 - this is a DEBUG log....
Why "my log test using INFO...." is shown twice even though I only called logger.info once?
Is this behavior expected? If yes why?
You added two sinks. The first one with level="INFO", which means all messages of level "INFO" or higher will be logged the sink, the second one with level="DEBUG", which means all messages of level "DEBUG" or higher will be logged by the sink. When you call logger.info(), the message is logged by your two sinks, since "INFO" is higher than "DEBUG".
Keep in mind the level attribute is a minimum threshold. The vast majority of logging frameworks work in this way, allowing you to adjust the level of log detail to suit your needs.
If you want two different formats depending on the severity level, you can use a dynamic function:
def formatter(record):
if record["level"].name == "DEBUG":
return DEBUG_FORMAT + "\n{exception}"
else:
return INFO_FORMAT + "\n{exception}"
logger.add(sys.stderr, format=formatter, level="DEBUG")
Otherwise, you can also filter the logs so that the sink receive only messages from a specific level:
logger.add(sys.stderr, format=DEBUG_FORMAT, level="DEBUG", filter=lambda record: record["level"].name == "DEBUG")
Hello, i have a similar question, i want to process several loggers with different type of message formatting, but i have found same problem - when message is emmited - it goes through all sinks, and is duplicated. I have not found any option, like in sntandard logging to turn off propagation of messagges from specific sink to others how can i achive this?
Hello @jekel.
Think of the logger as a basic facade that transmits the recorded message to the sinks. There is only one logger. There is no notion of parenting and therefore there is no propagation mechanism.
If you have messages that are only intended for a certain sink, then you need to configure your other sinks to ignore these messages. This is achieved by combining bind() with filter, see examples in the documentation.
If you need further help, feel free to create a new ticket.