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

Using custom logger across multiple modules #1162

Open
EtagiBI opened this issue Jun 26, 2024 · 1 comment
Open

Using custom logger across multiple modules #1162

EtagiBI opened this issue Jun 26, 2024 · 1 comment

Comments

@EtagiBI
Copy link

EtagiBI commented Jun 26, 2024

Hello,

I have a programme with a bunch of modules. In the main module a have a custom logger:

    from loguru import logger


    fmt = "[{time}] [{level}] - {extra} {name}:{function}:{line} - {message}"
    logger.remove()
    logger.add(format=fmt, sink="path/to/file")
    custom_logger = logger.bind(param_1=var_1, param_2=var_2)

I want to use my custom logger to catch all errors within all modules. For base logger I could just import logger in all modules and then use decorarator @logger.catch for all functions. What would be the best practice for custom logger? Should I pass it as the argument to submodules' functions and then explicitly call custom_logger.error() within except blocks?

    from loguru import logger

    import submodule_1

    fmt = "[{time}] [{level}] - {extra} {name}:{function}:{line} - {message}"
    logger.remove()
    logger.add(format=fmt, sink="path/to/file")
    custom_logger = logger.bind(param_1=var_1, param_2=var_2)
    submodule_1.run(custom_logger)
@Delgan
Copy link
Owner

Delgan commented Jun 29, 2024

Hi @EtagiBI.

Is your custom_logger dedicated solely to catching exceptions, or does it have other uses?

Depending on your use case, you may setup your custom parameters globally using configure():

logger.configure(extra={"param_1": var_1, "param_2": var_2})

Then, the parameters will appear in the logged message even when you use the base logger from your modules.

Another alternative is to dynamically patch() the record entries if there is an exception:

def patcher(record):
    if record["exception"] is not None:
        record["extra"].update(param_1=var_1, param_2=var_2)

logger.configure(patcher=patcher)

Again, the advantage is that you can directly use the base logger in all your modules.

Otherwise, if you need to give specific access to custom_logger, the best practice is as you suggested to pass it as an argument of your functions, or to define it in a specific module and then import it in your modules:

from my_logging_config import custom_logger

@custom_logger.catch
def my_func():
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants