question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to customize logging format (adding newlines and custom text in terminal)

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, May 28, 2021

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() after logger.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).

0reactions
ClimenteAcommented, May 28, 2021

Awesome!

Adding log.remove(0) before log.add() works as expected 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found