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

Use different log levels for different loggers #1126

Open
Snawe opened this issue Apr 19, 2024 · 2 comments
Open

Use different log levels for different loggers #1126

Snawe opened this issue Apr 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@Snawe
Copy link

Snawe commented Apr 19, 2024

Hi!

I wanted to try out loguru instead of the standard logging. However, I have one issue where I kinda find no solution to it.

My main issue is that I have multiple loggers. I use one for my application and two more for libraries (like logging.getLogger("uvicorn.access")). My main application logs on INFO, while for the libraries I use WARNING.

So for example currently I have

  • My application logging to console with INFO and to file (application.logs) with INFO
  • the uvicorn logs are with WARNING to console and INFO to uvicorn.logs
  • Both have different formatter.

I tried with loguru something like

logger.add(sys.stderr, level=logging.INFO)
logger.add("logs/application.log", level=logging.INFO)

for _log in ["uvicorn.access", "uvicorn", "uvicorn.error", "fastapi"]:
    # Found here on some issue I guess.
    _logger = logging.getLogger(_log)
    _logger.handlers = [InterceptHandler(_log)]
    logger.add(f"logs/{_log}.log", level=logging.INFO)
    logger.add(sys.stderr, level=logging.WARNING)

but thats obviously not possible.

So basically I want to have different log levels on different loggers. I tried the filter but couldn't make it work. I either get no logs or all logs in all files.
In addition I would also like to be able to:

  • Put each application/library to it's own log file
  • Have different extra/format for each application/library

Is this in general possible with loguru?

@Delgan
Copy link
Owner

Delgan commented May 2, 2024

I think this is possible although this may require some tweakering of your configuration.

For example, I see that you calling logger.add(sys.stderr) multiple times which is likely a mistake, as it would cause duplicated logs.

Which kind of filter did you try?

For example, if you want to log to the console with INFO by default, but with WARNING for Uvicorn, you could use something like that:

filtering_dict = {
    "": "INFO",
    "uvicorn": "WARNING",
    "fastapi": "WARNING",
}

logger.add(sys.stderr, filter=filtering_dict)

@Delgan Delgan added the question Further information is requested label May 2, 2024
@Snawe
Copy link
Author

Snawe commented May 6, 2024

Hey!

Thanks for your reply! Sorry, I am a bit busy atm. I will try out your posted filter and provide more information once I visit that topic again. Hopefully within the next few weeks.

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