Loguru: How to set the log level?

Created on 28 Aug 2019  路  10Comments  路  Source: Delgan/loguru

It's strange for something that is supposed to be stupidly simple there is no setLevel() function available, as on the plain old logger I get back from getLogger().

The docs show a heavy handed approach of adding a whole new handler just to set the level like:

logger.add(sys.stderr, format="{time} {level} {message}", filter="my_module", level="INFO")

But really?

question

Most helpful comment

I you would like to use another level in place of the default "DEBUG", you can just set the LOGURU_LEVEL environment variable to the severity level your prefer.

Alternatively, you can just re-add the stderr handler with the appropriate level, you don't need to modify the format and filter attributes:

logger.remove()
logger.add(sys.stderr, level="INFO")

All 10 comments

I you would like to use another level in place of the default "DEBUG", you can just set the LOGURU_LEVEL environment variable to the severity level your prefer.

Alternatively, you can just re-add the stderr handler with the appropriate level, you don't need to modify the format and filter attributes:

logger.remove()
logger.add(sys.stderr, level="INFO")

The thing to understand is that, contrary to standard logging, the Loguru's logger is not associated to any level: only handlers are. So, there is no setLevel() function because it would not make sense. The handlers are the sole master of the logs severity they accept.

Feel free to re-open this issue if you have others concerns.

Even though I set the LOGURU_LEVEL=INFO in .env logger still prints debug statements, could someone please suggest what could be going wrong here?

from loguru import logger
from dynaconf import settings

# init configs
var = settings.get("var")

logger.info(f"LOGURU_LEVEL: {os.environ['LOGURU_LEVEL']}") >> LOGURU_LEVEL: INFO
logger.debug("This shouldn't appear") >> This shouldn't appear

when I run the script as LOGURU_LEVEL=INFO python -m test_loguru the debug statement doesn't appear.

I would think this is a dynaconf issue, but the os.environ reports the correct environment variable for LOGURU_LEVEL.

@ab-10 Try importing dynaconf before loguru maybe?

Loguru setups logging values at import time using environment variables. If the environment variables have not been configured yet by dynaconf, Loguru will use the default values (which is "DEBUG" for logging level).

@ab-10 Try importing dynaconf before loguru maybe?

Loguru setups logging values at import time using environment variables. If the environment variables have not been configured yet by dynaconf, Loguru will use the default values (which is "DEBUG" for logging level).

That makes sense, however could you please advise why does importing loguru after the os environment variable has been set doesn't resolve the issue?

@ab-10 What do you mean? Are you saying that inverting dynaconf with louru import did not solve the problem?

yes, exactly

@ab-10 According to the dynaconf documentation, it seems environment variables are lazily loaded. The os.environ is not populated until settings.get() is called. So, if loguru is imported before settings being used, it will not have access to the configured LOGURU_LEVEL value.

Thanks @Delgan, this really looks like a dynaconf issue. I ended up using conda for setting the environment variable (in etc/conda/activate.d/env_vars.sh) and it works as expected now.

Was this page helpful?
5 / 5 - 1 ratings

Related issues

talz picture talz  路  6Comments

HarveySummers picture HarveySummers  路  4Comments

AllanLRH picture AllanLRH  路  6Comments

Gopichand995 picture Gopichand995  路  4Comments

zhouxiaomao picture zhouxiaomao  路  6Comments