3👍
Add ‘propagate’: False loggers.
'loggers': {
'django': {
'handlers': ['applogfile', 'console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
# prevent duplicate log in console
'propagate': False,
}
}
0👍
I tested your code in Django 3.2.7 if remove the root object it works fine and you also don’t have the duplicate lines.
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'
}
},
'loggers': {
'django': {
'handlers': ['applogfile', 'console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
},
}
- [Django]-What file or module actually generates "It worked" in Django tutorials?
- [Django]-Serving many on-the-fly generated images with Django
- [Django]-Django Rest Framework IsAuthenticated permission Error Anonymous user
- [Django]-Django logging: Cannot create logs per day
- [Django]-How to make email field required in the django user admin
0👍
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.
- [Django]-Permissions on my Model for Django
- [Django]-Counting distinct elements in Django ArrayField
- [Django]-Django Channels – Websocket connection failed
- [Django]-I keep getting the following error "local variable 'total_results' referenced before assignment"