Skip to content

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

Closed
@wolph

Description

@wolph

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions