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.

Can I customize log format on top of loguru format

See original GitHub issue

I am trying to figure out if I could customize log message in addition to the log format provided by loguru.

For example: Log message from Loguru : 2019-07-01 16:21:16.733 | INFO | __main__:run_server:50 - Tornado server starting on port 5000

Can I change this format to include correlation ID as well like 👇 [2019-06-28 09:51:59] [application_name] [correlationID] [INFO] - __main__:run_server:50 Tornado server starting on port 5000

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

19reactions
Delgancommented, Jul 2, 2019

I forgot to precise: you need first to remove() the default handler, otherwise, logs will be printed twice as you saw!

The thing is that, for convenience, Loguru adds a pre-configured handler which sends messages to stderr. This is why you can do from loguru import logger then logger.info("A message") and it works out of the box.

If you want to customize the log format, you have to remove the default handler first.

logger.remove()  # All configured handlers are removed
logger.add(sys.stderr, format=your_format)
logger.info("Logged message")

Note that the default format used by Loguru is as follow:

"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
10reactions
AnesBenmerzougcommented, Jul 6, 2019

Another way to do the same, which I use myself, is to directly configure the logger with the handler(s) you desire which also takes care of removing the default handler.

import sys
from loguru import logger

fmt = "[{time}] [application_name] [correlationId] [{level}] - {name}:{function}:{line} - {message}"
config = {
    "handlers": [
        {"sink": sys.stderr, "format": fmt},
    ],
}
logger.configure(**config)

logger.info("Logged message")

Which would give you the desired output:

[2019-07-06T11:35:07.697112+0200] [application_name] [correlationId] [INFO] - __main__:<module>:14 - Logged message
Read more comments on GitHub >

github_iconTop Results From Across the Web

Logger - loguru documentation - Read the Docs
The default values of sink parameters can be entirely customized. This is particularly useful if you don't like the log format of the...
Read more >
loguru Documentation - Read the Docs
How to set up logs formatting? How to filter messages? ... Using the logger in your scripts is easy, and you can configure()...
Read more >
A quick guide to using Loguru - Medium
The format parameter specifies the custom format of our log. We can ... Other than the keys which the logger provides, we can...
Read more >
Two Logging Options Better than Print Statements
There's lots more to customize with loguru . For example, you can change the default format using an environment variable so you don't...
Read more >
documentation
Loguru provides a set of functions and macros to aid logging to one or several ... By default, Loguru will only let you...
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