Capturing and replaying logs from nested functions
See original GitHub issueHi! 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >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
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 uselogger.log(42, "Foobar")
somewhere in your code thenrecord["level"].name
is equals to"Level 42"
but such level is not registered. This becomes a little bit more verbose unfortunately.You can
patch()
thelogger
before re-sending the log message in order to update therecord
dict: