Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concise way to change django console log level

Tags:

logging

django

I just want to be able to override the console log level in the settings file. I read the django logging document, but I'm having trouble making the logging do what I want. The documentation assures me that:

"From Django 1.5 forward, the project’s logging configuration is merged with Django’s defaults, hence you can decide if you want to add to, or replace the existing configuration. To completely override the default configuration, set the disable_existing_loggers key to True in the LOGGING dictConfig. Alternatively you can redefine some or all of the loggers."

So I tried just adding the following to my settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console':{
            'level': 'DEBUG',
        },
    },
}

...but I get an exception:

<snip>
  File "/usr/lib/python2.7/logging/config.py", line 575, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'console': 'NoneType' object has no attribute 'split'

Fair enough. It seems to want the whole configuration block. So I tried what I thought would be the simplest console logger config:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console':{
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'default': {
            'handlers': ['console'],
            'level': 'INFO',
            'filters': []
        }
    }
}

My intention is to set the log-level to INFO, but I still see a bunch of DEBUG messages, and the string MYFORMATTER doesn't appear in any of them anyway.

Finally, with blind optimism, I attempted this:

from django.utils.log import DEFAULT_LOGGING
DEFAULT_LOGGING['handlers']['console']['level'] = 'INFO'

I must be missing something quite obvious here.

BTW, I'm using Django 1.5.1.

like image 449
user2076663 Avatar asked Jul 19 '13 16:07

user2076663


People also ask

How do you change log levels?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

How do you disable verbose error handling and logging in Django?

If you don't want to configure logging at all (or you want to manually configure logging using your own approach), you can set LOGGING_CONFIG to None . This will disable the configuration process for Django's default logging.

How do you define logging level?

What Is a Logging Level. A log level or log severity is a piece of information telling how important a given log message is. It is a simple, yet very powerful way of distinguishing log events from each other. If the log levels are used properly in your application all you need is to look at the severity first.

What is the default logging level?

The default level for all loggers is Inherit, and the default level for the root logger is Info. Do not turn on Debug or higher logging without direction from technical support. Turning on this excessive logging for high volume module like system, query, or exec can rapidly flood your system and terminate the servers.


1 Answers

Answering my own question here, I ended up going with the following in the settings.py file:

import logging

logging.basicConfig(
    level = logging.INFO,
    format = " %(levelname)s %(name)s: %(message)s",
)
like image 96
user2076663 Avatar answered Oct 09 '22 18:10

user2076663