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.

[Question] How to use Loguru defaults + and extra information?

See original GitHub issue

Hi, I’m still researching about Loguru, but I can’t find an easy way to do this. I want to use the default options from Loguru, I believe they are great, but I want to add information to it, I want to add the IP of a request that will be logged.

If I tried this:

import sys
from loguru import logger
logger.info("This is log info!")
# This is directle from Loguru page
logger.add(sys.stderr, format="{extra[ip]} {extra[user]} {message}")
context_logger = logger.bind(ip="192.168.0.1", user="someone")
context_logger.info("Contextualize your logger easily")
context_logger.bind(user="someone_else").info("Inline binding of extra attribute")
context_logger.info("Use kwargs to add context during formatting: {user}", user="anybody")

That logs this: 1yVgp

I know that with logger.remove(0) I will remove the default logs, but I want to use it to obtain something like this: 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody, with XXX.XXX.XX.X being the IP. Using the default config (for color and the rest of thing) and adding a little thing to the format.

I’m trying to access the default configs, but I haven’t been able to import them and use them with logger.add. I think I will have to configure everything from scratch.

Hope someone can help me, thanks.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Delgancommented, Feb 6, 2022

Hi @antusystem.

I think you simply need to add() your handler using a custom format containing the extra information. Here is an example:

logger_format = (
    "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
    "<level>{level: <8}</level> | "
    "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
    "{extra[ip]} {extra[user]} - <level>{message}</level>"
)
logger.configure(extra={"ip": "", "user": ""})  # Default values
logger.remove()
logger.add(sys.stderr, format=logger_format)
1reaction
Delgancommented, Feb 9, 2022

Indeed, you need to specify level="TRACE" if you want all logs of "TRACE" level or above to be propagated to the handler (the default is "DEBUG").

You can use bind() where you need to set an user locally and configure() to set the default value globally. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Loguru defaults + and extra information?
I'm trying to access the default configs, but I haven't been able to import them and use them with logger.add . I think...
Read more >
Logger - loguru documentation - Read the Docs
It should be noted that extra values set using this function are available across all modules, so this is the best way to...
Read more >
A Complete Guide to Logging in Python with Loguru
Learn how to install, configure, and use the Loguru framework for logging in Python applications.
Read more >
loguru Documentation - Read the Docs
All sinks added to the logger are thread-safe by default. ... context_logger.info("Use kwargs to add context during formatting: {user}", ...
Read more >
A quick guide to using Loguru - Medium
Extra information · 1. All contextual information is stored in the record dictionary. · 2. The logger can be used along with python...
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