1

I am trying to log INFO as well as ERROR log on production. But I am not sure how to mention both log levels for the same project.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['console', ],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'sns': {
            'level': 'ERROR',
            'class': 'project.abc.snshandler.SNSHandler',
            'formatter': 'verbose'

        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console', ],
            'propagate': False,
        },
        'django.security.DisallowedHost': {
            'level': 'ERROR',
            'handlers': ['console', ],
            'propagate': False,
        },
        'project': {
            'level': 'ERROR',
            'handlers': ['console', 'sns'],
            'propagate': False,
        },

    },
}

At project level i want to log INFO logs as well.

Wagh
  • 4,202
  • 5
  • 39
  • 62

1 Answers1

4

The logging level you specify in your configuration is the minimum level will be logged. Just to recap, here the predefined levels in order of importance (from lowest to maximum):

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

So, if you put INFO in your configuration you will see all logs marked as INFO, WARNING, ERROR, and CRITICAL.

If you really want to see only INFO and ERROR you can try to write some custom code, but I discourage it as it smells as a bad design.

References:

Edit

Here how your configuration would look like:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['console', ],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'sns': {
            'level': 'ERROR',
            'class': 'project.abc.snshandler.SNSHandler',
            'formatter': 'verbose'

        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'INFO',
            'handlers': ['console', ],
            'propagate': False,
        },
        'django.security.DisallowedHost': {
            'level': 'INFO',
            'handlers': ['console', ],
            'propagate': False,
        },
        'project': {
            'level': 'INFO',
            'handlers': ['console', 'sns'],
            'propagate': False,
        },

    },
}
Nicholas
  • 471
  • 3
  • 13
  • Here I don't want INFO logs to get notified by SNS. Only ERROR logs should get notified by SNS. – Wagh May 21 '18 at 09:03
  • I see. So set `INFO` in `console` and `ERROR` in `sns`. I updated my suggested configuration code. – Nicholas May 21 '18 at 09:22
  • to clarify my edit: all logs from `project` of level `INFO` and above are handled by both `sns` and `console`, but `sns` discards all logs that are not `ERROR` or above. – Nicholas May 21 '18 at 09:24