0

I want to keep the convention of naming loggers for their module with logger = logging.getLogger(__name__), but have a single logger that captures them all.

Is that possible, and what should the logger's name be?

(I do not want to switch everything to logger = logging.getLogger('django'))

lofidevops
  • 15,528
  • 14
  • 79
  • 119

1 Answers1

2

Yup, the default logger is the root logger '', you can get it via:

Note the different type:

>>> logging.getLogger('')
<logging.RootLogger object at 0x7f0b9521ec18>

in Django you can add handlers to the root logger easily:

LOGGING = {
    ...
    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['system_log', 'debug_log', 'sentry'],
        },
    },
}

If you attach a handler to the root logger, it will see ALL the logs. This is not always wanted. So if you attach some handlers to a more specialised logger, you can then set the other one to propagate=False so that the message doesn't reach the root logger.

    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['system_log', 'debug_log', 'sentry'],
        },
        'django': {
            'level': 'INFO',
            'handlers': ['another_handler_just_for_django'],
            'propagate': False,
        },
   },
Kos
  • 70,399
  • 25
  • 169
  • 233