Skip to content

Best way to increase log level for specific module without duplicate messages #773

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

Closed
wolph opened this issue Jan 5, 2023 · 2 comments
Closed
Labels
question Further information is requested

Comments

@wolph
Copy link

wolph commented Jan 5, 2023

I'm trying to set a specific module to log level DEBUG while keeping the other logs at the normal level but I can't find a "good" solution for that.

I've been reading through the documentation and other issues and I haven't found anything that matches this case. There are a few useful tips in #138, but that doesn't solve the issue either.

Specifically what I'm looking for is something like this (with the standard logging module):

import logging

root_logger = logging.root
root_logger.setLevel(logging.INFO)

some_logger = logging.getLogger('some_debug_module')
some_logger.setLevel(logging.DEBUG)

With loguru I can do this:

import sys
from loguru import logger

logger.add(sys.stderr, level='INFO')
logger.add(sys.stderr, level='DEBUG', filter='some_debug_module')

While that works, it causes duplicate output for INFO and up.

I could do a custom filter, but that seems a rather clunky solution:

import sys
from loguru import logger


def filter(record):
    if record['level'].no >= logger.level('INFO').no:
        return True
    elif record['name'].startswith('some_debug_module'):
        return True
    else:
        return False
    
logger.add(sys.stderr, level='DEBUG', filter=filter)

Is there a better way to take care of this scenario? It seems like this would be a common use-case :)

@Delgan
Copy link
Owner

Delgan commented Jan 6, 2023

Hi.

You can pass a dict matching module name to minimum level to the filter argument (see docs):

filtering = {
    "": "INFO",  # Default.
    "some_debug_module": "DEBUG",
}

logger.add(sys.stderr, level="DEBUG", filter=filtering)

@Delgan Delgan added the question Further information is requested label Jan 6, 2023
@wolph
Copy link
Author

wolph commented Jan 6, 2023

Ah, fantastic. That's a great solution.

Thank you!

@wolph wolph closed this as completed Jan 6, 2023
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