How to customize logging format (adding newlines and custom text in terminal)
See original GitHub issueI’m trying to customize the logging format but probably the extra new lines and words not in tags and curly braces are removed.
Log file has the needed format but in terminal the extra newlines and text are ignored.
from loguru import logger as log
log_format = """<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> |
Module:<cyan>{name}</cyan>
Function:<cyan>{function}</cyan>
Line:<cyan>{line}</cyan>
{level}:
<level>{message}</level>
"""
log.add(
"app.log",
rotation="monthly",
format=log_format
)
# log = log.patch(lambda record: record["extra"].update(newline="\n"))
log = log.patch(lambda record: record["extra"].update(double_newline="\n\n"))
I tried something with using patch
, but it seems to add them at the end of the log and if many results in a key error.
How can I add some newlines (\n\n) and some custom text to format
?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to customize logging format (adding newlines and custom text ...
Log file has the needed format but in terminal the extra newlines and text are ignored. from loguru import logger as log log_format...
Read more >How to insert newline in python logging? - Stack Overflow
I have two solutions, the first is very easy, but the output is not very clean. The second method will produce the exact...
Read more >Log formatters which use newlines to separate messages ...
In Twisted's system, the "text" log formatter is only used if you're logging to a literal TTY, just to help with legibility and...
Read more >Configuring Logging - Quarkus
You can configure the format for each log handler via a dedicated property. For the console handler, the property is quarkus.log.console.format . The...
Read more >Code snippets and recipes for loguru
Those are used to configure the format of your handler. By creating a appropriate formatting function, you can easily define colors depending on...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The
logger.remove()
method will remove all handlers added so far. In particular, it will remove the default handler which causes logs to be duplicated on console.You have to call
logger.add()
afterlogger.remove()
, otherwise it’ll remove the handler you previously added. Another possibility is to remove only the default handler by specifying its identifier:logger.remove(0)
.Awesome!
Adding
log.remove(0)
beforelog.add()
works as expected 😃