2

i've my django log definition in the settings.py file as below:

LOG_DIR = '/var/log/myapp/'
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' if DEBUG else 'WARNING',
            'class':'logging.NullHandler',
        },
        'logfile': {
            'level':'DEBUG' if DEBUG else 'WARNING',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': LOG_DIR + "/application.log",
            'maxBytes': 1024 * 1024 * 10, #Max 10MB
            'backupCount': 3,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG' if DEBUG else 'WARNING',
            'propagate': False,
        },
        '': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    }
}

Now, i have several applications in this project, and i have to organize the logging of them in a simple way, creating a separate log from each other, i mean:

  • Project general logs
  • My_App1 logs
  • My_App2 logs
  • My_App3 logs

Is this possible in a easy way with Django?

Symon
  • 673
  • 6
  • 25
  • 1
    You will have to add more loggers pointing to seperate handlers or write a handler that can use seperate files per app. – Klaus D. Dec 08 '18 at 11:41

1 Answers1

4

Under 'loggers', you need an entry for each app:

'app1': {
            'handlers': ['app1'],
        },

And likewise under 'handlers':

'app1': {
        'level':'DEBUG' if DEBUG else 'WARNING',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': LOG_DIR + "/app1.log",
        'maxBytes': 1024 * 1024 * 10, #Max 10MB
        'backupCount': 3,
        'formatter': 'standard',
},
Trevor Cox
  • 187
  • 6