Loguru: How do I keep the console in the same format as the output to the file?

Created on 12 Dec 2020  路  2Comments  路  Source: Delgan/loguru

Hello, Delgan

I want to use the add () method to keep the log format of the console and output files consistent, as follows:

logger.add(sys.stderr, format="{time:YYYY-MM-dd HH:mm:ss.SSS} [{level}] [{file.name}]: {message}")
logger.add("file.log", format="{time:YYYY-MM-dd HH:mm:ss.SSS} [{level}] [{file.name}]: {message}")
logger.debug("This's a log message")

But when output to the console, there will be two logs, and the first log is loguru by default. Is there any way I can avoid this? Thank you very much!!!
The console log:

2020-12-12 19:17:41.419 | DEBUG    | __main__:<module>:7 - This's a log message
2020-12-55 19:17:41.419 [DEBUG] [log_demo.py]: This's a log message

Most helpful comment

Hey @hege66. You need first to .remove() the default handler. :+1:

logger.remove()  # Will remove all handlers added so far, including the default one
logger.add(sys.stderr, format="{time:YYYY-MM-dd HH:mm:ss.SSS} [{level}] [{file.name}]: {message}")
logger.add("file.log", format="{time:YYYY-MM-dd HH:mm:ss.SSS} [{level}] [{file.name}]: {message}")
logger.debug("This's a log message")

All 2 comments

Hey @hege66. You need first to .remove() the default handler. :+1:

logger.remove()  # Will remove all handlers added so far, including the default one
logger.add(sys.stderr, format="{time:YYYY-MM-dd HH:mm:ss.SSS} [{level}] [{file.name}]: {message}")
logger.add("file.log", format="{time:YYYY-MM-dd HH:mm:ss.SSS} [{level}] [{file.name}]: {message}")
logger.debug("This's a log message")

@Delgan
Hello, Delgan

Very fast and accurate answer, thank you very much for taking time out of your busy schedule to reply, thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonathanhle picture jonathanhle  路  3Comments

Gopichand995 picture Gopichand995  路  4Comments

af6140 picture af6140  路  5Comments

sergree picture sergree  路  3Comments

ghost picture ghost  路  4Comments