Django logging - how does propagate work?
I have a following logging config:
"root": {
"level": "ERROR",
"handlers": ["console", "server_file"],
},
"handlers": {
"celery_file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"filename": os.path.join(ROOT_LOGS_DIR, "celery", "celery.log"),
"formatter": "standard",
"maxBytes": 1024 * 1024 * 50, # 50 MB
"backupCount": 30,
},
"loggers": {
# '': {'handlers': ["root"], "level": "DEBUG", "propagate": True, },
'django': {'handlers': ['django'], 'level': 'DEBUG', 'propagate': False, },
"celery": {
"handlers": ["celery_file"],
"level": "INFO",
"propagate": True|False,
},
In application,
log = logging.getLogger("celery.inbound_outbound_jobs")
print(log.handlers) # output: [] No handlers
log.error("test")
My question is :
- when celery logger propagate =True, will this log be handled by celery logger? Also does it propagate to root logger?
- when propagate=False, will log be still handled by celery logger?
Answer 1.
since propagate = True, any log message from celery.inbound_outbound_jobs will bubble up to its parent (celery) and then to the root logger if needed. If the root logger also had appropriate handlers (e.g., "server_file"), the logs would propagate to the root as well
Answer 2.
The celery.inbound_outbound_jobs logger will still inherit from celery because it's not explicitly defined. However, since propagate = False, logs will not be forwarded to the root logger.
The message will only be logged in
celery.log(viacelery_file).It will not reach the root logger and thus won't be written to
server.log.
If you want to directly attach handlers to "celery.inbound_outbound_jobs", explicitly define it in the loggers section.
"loggers": {
"django": {"handlers": ["django"], "level": "DEBUG", "propagate": False},
"celery": {"handlers": ["celery_file"], "level": "INFO", "propagate": True},
"celery.inbound_outbound_jobs": {
"handlers": ["celery_file"],# here you can attach handlers explicitly
"level": "INFO",
"propagate": False, # here prevent the unnecessary duplication
},
}