Delgan / loguru

Python logging made (stupidly) simple
MIT License
19.43k stars 690 forks source link

Allow using `opt` method as a context manager #1168

Open rusmux opened 2 months ago

rusmux commented 2 months ago

Would be nice to be able to use opt method as a context manager like this:

from loguru import logger

def log() -> None:
    logger.info("Hello, World!")

with logger.opt(depth=1):
    log()
Delgan commented 2 months ago

The opt() was meant to only adjust one logged message, using it as a context manager would go against this design choice.

I'm not convinced of the usefulness of such feature with the given example here. It's better not to let depth being set implicitly like that by the parent in my opinion.