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.

Capturing and replaying logs from nested functions

See original GitHub issue

Hi! I just started using loguru this week and there is a specific behaviour I want to achieve but I’m not sure how to search for it. Let me provide a very simple example of what I’m talking about:

I have a function that may output logs

def bark():
    logger.info('whoof')

In another part of the code, I want to capture the log and add something before it:

# intercept message coming from bark
bark()
# add something or log something before it
logger.info('A dog will bark')
# replay message from bark

The final output I’m looking for is:

A dog will bark
whoof

The example heavily simplifies my use case, here calling logger.info('A dog will bark') before bark() would solve the issue, but for my use case that is not possible


I took a look at InterceptHandler but I don’t think that is related. I’m guessing there is a way to use sinks to achieve this, but I’m not sure how.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Delgancommented, Oct 13, 2020

Yeah, that’s because we are calling logger.log() using an “anonymous” level: record["level"].no. We are just passing a level no which is not associated to any color.

We need to pass the record["level"].name instead, but we need first to make sure that such level exist. For example, if you use logger.log(42, "Foobar") somewhere in your code then record["level"].name is equals to "Level 42" but such level is not registered. This becomes a little bit more verbose unfortunately.

for msg in self.deferred:
    record = msg.record
    patched = logger.patch(lambda r: r.update(record))
    level = record["level"].name

    try:
        logger.level(level)
    except ValueError:
        # If the level 'name' is not registered, use its 'no' instead
        level = record["level"].no
        
    patched.log(level, record["message"])
1reaction
Delgancommented, Oct 11, 2020

You can patch() the logger before re-sending the log message in order to update the record dict:

for msg in self.deferred:
    record = msg.record
    patched = logger.patch(lambda r: r.update(record))
    patched.log(record["level"].no, record["message"])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does an equivalent of override exist for nested functions?
Here's one way of doing it, creating a new foo that "does the right thing" by hacking the function internals. ( As mentioned...
Read more >
Variable scope, closure - The Modern JavaScript Tutorial
Here the nested function getFullName() is made for convenience. It can access the outer variables and so can return the full name.
Read more >
Logging with nested functions · Issue #416 - GitHub
My issue is I have logging set up in two functions, both using Write-PSFMessage with the LogFile provider, each has their own log...
Read more >
US7120901B2 - Method and system for tracing and displaying ...
A method and system for tracing the failing or successful execution of nested functions coded with return codes in a thread during its...
Read more >
Nested Remote Functions — Ray 2.2.0
Remote functions can call other remote functions, resulting in nested tasks. For example, consider the following. import ray @ray.remote def f(): return 1 ......
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