1

I'm using Django 1.4.1. There's a script tmp.py in the same direcotry as manage.py, it does some routime operatons and is started from cron with command like python /path/to/project/tmp.py

Here is tmp.py:

import os
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MYPROJECT.settings")

if __name__ == "__main__":
    import logging
    logger = logging.getLogger('parser.test')
    logger.info('Hello, world')

This script makes a log entry, but this entry is just ignored by logger. When I put same 3 lines into any of my views, log entry appears in my log file as expected.

Logger config from setings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': "/hosting/MYPROJECTS/logs/logfile",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'parser': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    }
}

Why this logger works only in views? Maybe logger is still not configured after os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MYPROJECT.settings") in my tmp.py?

Antonio
  • 822
  • 1
  • 10
  • 19
  • what happens if you put `print`? – EsseTi Mar 11 '13 at 15:22
  • what is in logger.handlers? the environ variable is not enough to setup django you need also to call something extra look this: https://docs.djangoproject.com/en/1.4/topics/settings/#custom-default-settings – Jorge E. Cardona Mar 12 '13 at 04:24
  • `os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MYPROJECT.settings")` is enough to load settings. After that I can use my models, connect to db, etc. Read next caption in your link: "Either configure() or DJANGO_SETTINGS_MODULE is required". I made `DJANGO_SETTINGS_MODULE` case. – Antonio Mar 13 '13 at 07:47

2 Answers2

1

I can't solve this problem but I found alternative solution: 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.

Antonio
  • 822
  • 1
  • 10
  • 19
0

I managed to get Django logging settings to work for my non-Django script.

First, let's orient to the Django directory: <myproject>/<src>/<myproject>/settings.py & <myproject>/<src>/manage.py

# FILE: <myproject>/<src>/scripts/save_report_2_mongo.py
[...]
import logging
logger = logging.getLogger(__name__)
[...]

At the top of my script I import logging. Then I create the logging object. In my case __name__ == scripts.save_report_2_mongo. If the OP's setup is anywhere near to mine, then by this example name != "main", and the logger is never instantiated. Right?

Finally, inside settings.py

# FILE: <myproject>/<src>/<myproject>/settings.py
[...]
LOGGING = {
    [...]
    'formatters' : {...},
    'handlers' : {...},
    'loggers': {
       'django': {
           'handlers': ['console', 'fileInfo', 'fileDebug'],
           'level': 'DEBUG',
           'propagate': True,
       },
       'scripts.save_report_2_mongo': {
           'handlers': ['sr2m_handler'],
           'level': 'DEBUG',
       },
    }

I believe this works because of this passage in Python docs: docs.python.org > Logging HOWTO > Advanced Logging Tutorial

Advanced Logging Tutorial

The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.

  • Loggers expose the interface that application code directly uses.

    [...]

Logging is performed by calling methods on instances of the Logger class (hereafter called loggers). Each instance has a name, and they are conceptually arranged in a namespace hierarchy using dots (periods) as separators. For example, a logger named ‘scan’ is the parent of loggers ‘scan.text’, ‘scan.html’ and ‘scan.pdf’. Logger names can be anything you want, and indicate the area of an application in which a logged message originates.

A good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:

logger = logging.getLogger(__name__)

This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.

My emphasis--logger names track the package/module hierarchy. In this case I obtained the value of __name__ inside my script and then named the logger in the loggers section of settings.py.

xtian
  • 2,765
  • 7
  • 38
  • 65