Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate log output when using Django logging module

Tags:

logging

django

I am using the following logging snippet in my Django settings.py file. As seen in image below, all the logs are being captured twice.

The reason I kept root object as I wanted to capture logger.info() in console as well as log file

settings.py

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'simple': {
        'format': '[%(asctime)s] %(levelname)s|%(name)s|%(message)s',
        'datefmt': '%Y-%m-%d %H:%M:%S',
    },
},
'handlers': {
    'applogfile': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': os.path.join(BASE_DIR, 'django_blend.log'),
        'backupCount': 10,
        'formatter': 'simple',
    },
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'simple'
    }
},
'root': { # this tells to capture logger.info() to console as well as in log file
        'handlers': ['console', 'applogfile'],
        'level': 'INFO',
    },
'loggers': {
        'django': {
            'handlers': ['applogfile', 'console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
                }
        }
}

django.log

enter image description here

like image 234
Mahesh Avatar asked Jun 20 '26 19:06

Mahesh


2 Answers

Add 'propagate': False loggers.

'loggers': {
    'django': {
        'handlers': ['applogfile', 'console'],
        'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),

        # prevent duplicate log in console
        'propagate': False,
       }
    }
like image 126
Seyed Mostafa SeyedAshoor Avatar answered Jun 24 '26 13:06

Seyed Mostafa SeyedAshoor


It's worth reading the documentation for logging.Logger.propagate carefully. It offers a very clear explanation of the propagation mechanism. There's also a note in there that says:

Note: If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. In general, you should not need to attach a handler to more than one logger - if you just attach it to the appropriate logger which is highest in the logger hierarchy, then it will see all events logged by all descendant loggers, provided that their propagate setting is left set to True. A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.

(my emphasis)

The OP's example has duplicate handlers for the root and django loggers, so removing the duplicated handlers from the django logger should work.

If you need specific handlers for specific modules, you can add them explicitly and set propagate=False there, as explained e.g. here.

like image 31
djvg Avatar answered Jun 24 '26 13:06

djvg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!