Skip to content
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

Weird behaviour of logger after 'bind' #1191

Open
argrento opened this issue Aug 20, 2024 · 1 comment
Open

Weird behaviour of logger after 'bind' #1191

argrento opened this issue Aug 20, 2024 · 1 comment
Labels
question Further information is requested

Comments

@argrento
Copy link

Consider this code

import sys

from loguru import logger

print(f"foo module {logger=}")

class Foo:
    def __init__(self):
        l = logger.bind(source="foo")
        l.remove()
        l.add(sys.stderr)
        print(f"foo __init__ {logger=}, {l=}")

if __name__ == "__main__":
    f = Foo()

When I print it, I see the following:

foo module logger=<loguru.logger handlers=[(id=0, level=10, sink=<stderr>)]>
foo __init__ logger=<loguru.logger handlers=[(id=1, level=10, sink=<stderr>)]>, l=<loguru.logger handlers=[(id=1, level=10, sink=<stderr>)]>

Why id of logger also changed?

@Delgan
Copy link
Owner

Delgan commented Sep 22, 2024

Keep in mind there is only one global registry of the handlers. The logger is a facade toward these handlers. The bind() method is helpful to give context to messages logged by a logger. However, in the end, it will always end up to the same set of handlers, because they share the same reference to it.

If you really want to create separate logger that can manage handlers independently, refer to the documentation: Creating independent loggers with separate set of handlers.

@Delgan Delgan added the question Further information is requested label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants