madtyn
madtyn

Reputation: 1549

Django logging with several settings files

I'm having two problems with Django logging. I have this 3-tier project structure layout that I have written further down.

My two problems are:

1) I want only one unique logger for every app which writes to console. The only way of achieving this right now is adding app by app, as I'm doing right now. (I thought I would get this to work with the 'root' logger I modified. You can see the line in the settings file)

2) When coming to log inside one settings file, the console shows this warning:

No handlers could be found for logger "sonata.settings.local"

I'd like to be able to log at my settings files.

project structure layout

.
├── docs
├── requirements
├── scripts
└── sonata
    ├── person
    │   └── templatetags
    ├── registration
    ├── sonata
    │   └── settings
    │       ├── base.py
    │       ├── heroku.py
    │       ├── local.py
    |       └── production.py
    ├── static
    │   ├── css
    │   │   └── images
    │   ├── fonts
    │   └── js
    ├── templates
    │   ├── personApp
    │   └── registrationApp
    └── utils
        └── templatetags

logging at base.py (main settings file)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        '' : {
            'handlers':['console'],
            'level':'DEBUG',
        },
        'sonata.settings.heroku' : {
            'handlers':['console'],
            'level':'DEBUG',
        },
        'registration' : {
            'handlers':['console'],
            'level':'DEBUG',
        },
        'person' : {
            'handlers':['console'],
            'level':'DEBUG',
        }
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO'
    },
}

Upvotes: 0

Views: 619

Answers (1)

Daniel Hepper
Daniel Hepper

Reputation: 29967

For your logging configuration to be active, your settings have to be loaded. If you log in your settings file (or a module that is imported by it), your logging configuration is not yet loaded.

Have a look at django/__init__.py:

...
from django.conf import settings
from django.utils.log import configure_logging
...

The statement LOGGING = ... in your settings file doesn't actually configure the logging system. The configuration of the logging happens after the settings have been loaded.

You could configure logging manually in your settings file by directly using the logging module, but I would suggest you first evaluate if you really need to log in your settings file.

Upvotes: 1

Related Questions