1

I am trying to get a Logger working for my Django App. I call the logger with:

logger = logging.getLogger(__name__)
logger.info("some stuff")

inside a function in my views.py.

But it doesn't work, the file which is supposed to contain the log is created but nothing is written in it, here are the settings :

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters':{
    'standard': {
        'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
    },
},
'handlers': {
    'file_INFO': {
        'level': 'INFO',
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': os.path.join(BASE_DIR, 'logs/site.logs'),
        'maxBytes': 1024 * 1024 * 15,  # 15MB
        'backupCount': 10,
        'formatter': 'standard',
    },
    'console_INFO': {
        'level': 'INFO',
        'class': 'logging.StreamHandler',
    }
},
'loggers': {
    'INFO': {
        'handlers': ['file_INFO', ],
        'level': 'INFO',
        'propagate': True,
    },
},

}

Any idea ?

Thanks !

Nicolas
  • 11
  • 3

1 Answers1

2

You have named your logger 'INFO', but you have asked for logger __name__. So the logger you use is not the one you want.

Your best bet is to use the root logger, like this:

LOGGING = {
    ...
    'root': {
        'handlers': ['file_INFO', ],
        'level': 'INFO',
    },
}

All loggers will propagate to the root logger (unless told otherwise), and this setup is enough for more than 90% of applications. If you use the root logger, you also don't need to run getLogger(__name__), you can simply log with logging.info("some stuff").

Antonis Christofides
  • 6,990
  • 2
  • 39
  • 57
  • I add a missunderstanding of how the logger works. I thought that "__name__ " would be the name of an instance of a logger and that what I define in the logger settings are different behaviours that a logger can i have according to, for example, the level of a logging ticket or the name of the module it has been sent by. – Nicolas Dec 01 '16 at 09:06