[Solved]-Django rest framework logging different levels on different files

9👍

Here:

'loggers': {
    'django': {
        'handlers': ['fileInfo'],
        'level': 'INFO',
        'propagate': True,
    },
    'django': {
        'handlers': ['fileDebug'],
        'level': 'DEBUG',
        'propagate': True,
    },

You define the ‘django’ key twice, so the second overwrite the first.

As a general rule, if you want specific settings for a given logger, configure it as a distinct logger (ie one logger per package or django app). Also note that the logger’s “handlers” are lists so you can have more than one handler per logger (ie one for debug and one for info). The logging lib is a bit complex but taking time to read the full doc and experimenting a bit is worth the effort, really.

A couple other notes:

  1. using file handlers for multiprocess apps (in production django is most often served by multiprocess front servers) is more often than not a bad idea (concurrent write accesses to a file never really work).

  2. this 'filename': LOGGING_ROOT + "/info.log" kind of defeats the whole point of using os.path – you want os.path.join(LOGGING_ROOT, "info.log") instead

Leave a comment