loguru icon indicating copy to clipboard operation
loguru copied to clipboard

How to send different logs to std and log file?

Open Whisht opened this issue 2 years ago • 1 comments

I want to transform the logs into different formats in std and file. For example:

# in STD out
{level} : {time_elapsed} \n {retrievaled result}

# in the log file:
{
"query": {query}
"time": {time_elapsed}
"message": {some_other_message}
"result":{result}
}

Concretely, the logs in std and file are in different formats that json for file. The logged messages are different, as the file has a more detailed message some_other_message.

Whisht avatar Oct 19 '23 01:10 Whisht

You can simply call .add() twice, once for each output you need.

logger.remove()  

logger.add(sys.stderr, format="{level} {time} {message}"
logger.add("file.log", serialize=True)

You can also implement a custom sink or formatter if you need to. For example, see Serializing log messages using a custom function.

Delgan avatar Oct 20 '23 11:10 Delgan