Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11573

Django not logging anything levels less than WARNING

I am not sure why django is not logging anything less than "WARNING" level. I have this code in the view:

logger = logging.getLogger(__name__)

def profile_data(request):
    logging.info("INFO PROFILE DATA!!")
    logging.debug("DEBUG PROFILE DATA!!")
    logging.warning("WARNING PROFILE DATA!!")
    logging.error("ERROR PROFILE DATA!!")

and this in my settings.py:

# Logging
LOGGING = {
    'version': 1,
    # Version of logging
    'disable_existing_loggers': False,

    'filters': {
        # information regarding filters
    },

    'formatters': {
        '<simple_format>': {
            'format': '{levelname} {message}',
            'style': '{',
        }
    },

    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/logs/log_file1.log',
        },

        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler'
        },
    },

    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'DEBUG',
        },
        'root': {
            'handlers': ['file', 'console'],
            'level': 'DEBUG',
        }
    }
    }

As you can see, I try to set everything to the DEBUG level, it doesn't work, I only see this is the warning and error level in the terminal:

WARNING:root:WARNING PROFILE DATA!!
ERROR:root:ERROR PROFILE DATA!!

EDIT

changed the logger declaration to:

logger = logging.getLogger('app_logger')

the call to:

logger.info("INFO PROFILE DATA!!")

and the settings to the new name, of course:

'loggers': {
    'app_logger': {
        'handlers': ['file', 'console'],
        'level': 'DEBUG',
    },

But it still prints only WARNING and above. When am I declaring a new logger? shouldn't logging.getLogger() get the logger declared in the settings? how should I import that logger in my views?

Also, tried adding the logger to the top level of the dict key as suggested by an answer below ('app_logger': {"level": "DEBUG", "handler": "console"},) it didn't work.

Upvotes: 2

Views: 2037

Answers (2)

user1600649
user1600649

Reputation:

So the following config:

LOGGING = {
    "version": 1,
    "root": {"level": "DEBUG", "handlers": ["console"]},
    "loggers": {"oldname": {"level": "WARNING", "handlers": ["console"],}},
    "handlers": {
        "console": {"class": "logging.StreamHandler", "stream": "ext://sys.stdout"}
    },
}

Yields:

IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import logging

In [2]: logging.warning("open stdout")
WARNING:root:open stdout

In [3]: root = logging.getLogger(None)

In [4]: old = logging.getLogger("oldname")

In [5]: new = logging.getLogger("newname")

In [6]: print(root.level, old.level, new.level)
10 30 0

In [7]: root.debug("test")
DEBUG:root:test

In [8]: old.debug("test")

In [9]: new.debug("test")
DEBUG:newname:test

So moving your "root" key from inside "loggers" to the top-level of the LOGGING dict, solves your problem.

Note: The name "root" at top level is a reserved name to configure the root logger, a key with any other name at the top level won't work. And because undefined loggers inherit settings from root, your undefined logger will work.

With a defined logger

There's something else interfering with your configuration. Reduce your configuration to only this, then start adding stuff one by one until it breaks.

LOGGING = {
    "version": 1,
    "loggers": {"app_logger": {"level": "DEBUG", "handlers": ["console",]}},
    "handlers": {"console": {"class": "logging.StreamHandler",}},
}

Also, this lives in settings.py of your django project and it only applies if you open up a Django shell via python manage.py shell. Just using this in the standard interpreter won't load the config (just to cover the basics).

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477814

You constructed a new logger with:

logger = logging.getLogger(__name__)

__name__ has value [geeksforgeeks.org]:

If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module's name.

So you will either construct a logger with the name __main__, or one with the name of a module. But these are not the loggers you defined in the settings.py, this is a new logger.

Another problem is: you do not use the logger, you use logging, so the "root" logger, you thus should replace:

    logging.info('INFO PROFILE DATA!!')

with:

    logger.info('INFO PROFILE DATA!!')

We can for example make use of the root logger with:

import logging

logger = logging.getLogger('root')

def profile_data(request):
    logger.info('INFO PROFILE DATA!!')
    logger.debug('DEBUG PROFILE DATA!!')
    logger.warning('WARNING PROFILE DATA!!')
    logger.error('ERROR PROFILE DATA!!')

Upvotes: 3

Related Questions