0

I am trying to get started with Django and as a first toyproject I am using functionality to send emails and for logging in an existing application

Here's a small script:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

import logging
logger = logging.getLogger("myLogger")
logger.debug("A")
logger.info("B")
logger.error("C")

my settings.py sits in the very same folder with the following code fragment in there:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'tmp//debug.log',
        },
    },
    'loggers': {
        'myLogger': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

When executing (running the script, no Django server involved) it produces the output

No handlers could be found for logger "myLogger"

Any comments? I can use the standard logger but my suspicion is that I am missing something rather fundamental here...

Thomas

tschm
  • 2,905
  • 6
  • 33
  • 45
  • It might be irrelevant but is (the double slash in) your path correct for the log file ? – Ambroise Feb 13 '14 at 15:01
  • No difference when removing it... – tschm Feb 13 '14 at 15:17
  • See "write a custom admin command instead of separate script near manage.py https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ It logs works fine in admin commands." http://stackoverflow.com/questions/15340516/django-logging-in-script-near-manage-py-messages-are-just-ignored – fragles Feb 13 '14 at 15:41

2 Answers2

2

Thanks to pbacterio, that was a pointer into the right direction. This works for me

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

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

dictConfig(settings.LOGGING)
tschm
  • 2,905
  • 6
  • 33
  • 45
1

Try configure logging like django:

For Django 1.6:

from django.conf import settings
settings.configure()

For 1.7:

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

configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
pbacterio
  • 1,094
  • 6
  • 12
  • interesting, I think I am using Django 1.6. I don't have configure_logging in Django.utils.log. – tschm Feb 13 '14 at 15:35
  • import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") from django.conf import settings from django.utils.log import dictConfig dictConfig(settings.LOGGING) – tschm Feb 13 '14 at 15:45