Last active
October 15, 2024 11:15
-
-
Save wodCZ/c6ea066b3b9b50010ae5e569e48d3c9b to your computer and use it in GitHub Desktop.
attempt to solve https://stackoverflow.com/questions/54307831/how-to-enforce-logger-format-during-celery-task-execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from celery.app.defaults import DEFAULT_TASK_LOG_FMT, DEFAULT_PROCESS_LOG_FMT | |
class CeleryTaskFilter(logging.Filter): | |
def filter(self, record): | |
return record.processName.find('Worker') != -1 | |
class CeleryProcessFilter(logging.Filter): | |
def filter(self, record): | |
return record.processName == 'MainProcess' | |
class NotCeleryFilter(logging.Filter): | |
def filter(self, record): | |
return record.processName != 'MainProcess' and record.processName.find('Worker') == -1 | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { # Sets up the format of the logging output | |
'simple': { | |
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
'datefmt': '%y %b %d, %H:%M:%S', | |
}, | |
'celeryTask': { | |
'()': 'celery.app.log.TaskFormatter', | |
'fmt': DEFAULT_TASK_LOG_FMT | |
}, | |
'celeryProcess': { | |
'()': 'celery.utils.log.ColorFormatter', | |
'fmt': DEFAULT_PROCESS_LOG_FMT | |
}, | |
}, | |
'filters': { | |
'celeryTask': { | |
'()': CeleryTaskFilter, | |
}, | |
'celeryProcess': { | |
'()': CeleryProcessFilter, | |
}, | |
'notCelery': { | |
'()': NotCeleryFilter, | |
}, | |
}, | |
'handlers': { | |
'console': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'simple', | |
'filters': ['notCelery'] | |
}, | |
'console2': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'celeryTask', | |
'filters': ['celeryTask'] | |
}, | |
'console3': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'celeryProcess', | |
'filters': ['celeryProcess'] | |
}, | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['console', 'console2', 'console3'], | |
'level': 'DEBUG', | |
'propagate': False, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment