loguru
loguru copied to clipboard
How to send different logs to std and log file?
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.
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.