-
-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
capture_logs
doesn't affect already initalized BoundLogger
s
#408
Comments
May I ask how you ran into that problem? Because it's rather uncommon, I think, to much around with already bound loggers? Your approach would certainly have thread-wide side-effects, but I suspect that would be even desired? Thoughts @rgalanakis?
Thank you, I'm trying! 😅 |
Totally possible that I got a concept wrong. import structlog
logger = structlog.get_logger(__name__) And in that module a class that binds a local logger on class Something:
def __init__(self):
self.logger = logger.bind(some="thing")
def a_method(self):
self.logger.info("doing something") When I use @pytest.fixture
def something() -> Something:
return Something()
def test_something_a_method(something: Something):
with capture_logs() as events:
something.a_method()
assert events[0]['event'] == "doing something" # fail Is there a better way to do something like this? |
Well, yes. In my case that would be desired and imho I'd expect a testing utility like |
I would say that not capturing already-created loggers is an oversight in what I originally wrote. In similar logging libraries that I have used more extensively, in, say, Ruby, I expect it to capture all logs. In languages that are quite different and have no TLS (like Go) I would not expect global modifications. So I agree with @fabianbuechler here. |
Oh I agree with the idea too, I just wonder if there’s a conscious reason why you didn’t choose the reference approach. If there’s some edge cases I can’t think of right now or if something broke in weird ways. :) |
Unfamiliarity with structlog's internal implementation I suppose. |
@rgalanakis thank you for coming back to us and thank you again so much for adding this to structlog. I use it in all my projects nowadays! |
Hey, I found an issue with using
structlog.testing.capture_logs
that might be intentional, but tripped me up while testing.I have a workaround / patch that I could submit as PR, but wanted to ask first to clarify whether the behavior is intentional.
When using
capture_logs
the processors of any existing, already initializedBoundLogger
s aren't updated to useLogCapture
and their logs are thus not captured.I've written a bit of code to clarify what I mean:
That basically already contains my approach for a fix: keeping the
_Configuration.processors
list intact and only modify its contents so that theBoundLogger
s which get the list passed by reference are affected by the changes.Let me know if you agree that this change would be benefitial, then I can prepare a PR with tests.
And of course: thanks for building structlog - it's amazing!
The text was updated successfully, but these errors were encountered: