I try to log something with INFO level in the views.py in a created app in my Django project. When I initialize the logger with: logger = logging.getLogger(__name__)
the loglevel is set to 0 which means undefined. Why does it not read the log level defined in the settings py? I start the project in the pycharm IDE and a environment variable with name LOGLEVEL is set to INFO (I checked if the loglevel was correctly read by debugging the settings.py on startup and it is correct set to INFO)
Project Structure: myApp
- views.py
- ... myproject
- settings.py
- ...
settings.py loggin config:
# Disable Django's logging setup
LOGGING_CONFIG = None
LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper()
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
# Use JSON formatter as default
'default': {
'()': 'pythonjsonlogger.jsonlogger.JsonFormatter',
},
'django.server': DEFAULT_LOGGING['formatters']['django.server'],
},
'handlers': {
# Route console logs to stdout
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
},
'django.server': DEFAULT_LOGGING['handlers']['django.server'],
},
'loggers': {
# Default logger for all modules
'': {
'level': LOGLEVEL,
'handlers': ['console', ],
},
# Default runserver request logging
'django.server': DEFAULT_LOGGING['loggers']['django.server'],
}
}
views.py
logger = logging.getLogger(__name__)
def teams_overview(request):
try:
print(f"loglevel set: {logger.level}")
print(f"info logleve: {logging.INFO}")
logger.error("error")
logger.info("info")
logger.warning("warning")
logger.critical("critical")
logger.debug("debug")
output:
error
warning
critical
loglevel set: 0
info logleve: 20
Django==4.1.7
I expect the initialized logger to have the defined LOGLEVEL from the settings.py. Why is my logger undefined in this scenario?