Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way of selectively disabling request_starter / request_finished / task_started / task_succeeded / task_enqueued? #283

Closed
adrenaline681 opened this issue Aug 21, 2023 · 5 comments

Comments

@adrenaline681
Copy link

During development, the request_starter / request_finished / task_started / task_succeeded / task_enqueued logs start to clutter the command line.

Is there a way to selectively disable some of these logs?

For example:

  • Disable "request_finished" only
  • Disable "task_started" and "task_succeeded"
  • Disable all except "task_enqueued" (celery beat)
@jrobichaud
Copy link
Owner

Python has a log filtering mechanism. Have you considered it?

@adrenaline681
Copy link
Author

I've never used it but i will give it a try, thanks

@jrobichaud
Copy link
Owner

jrobichaud commented Aug 21, 2023

config/settings/my_filters.py (you might want to use a better file location)

# filters implementations


def event_blacklist(blacklist):
    # this filter remove the events included in the provided blacklist
    b = set(blacklist)
    def func(record):
        return record.msg["event"] not in b

    return func

config/settings/local.py

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": { # <- declaration of filters
        "event_blacklist": {
            "()" : "config.settings.my_filters.event_blacklist",
            "blacklist": ["request_finished"] # <- list of events to blacklist
        }
    },
    "formatters": {
    # ....

    "handlers": {
        "colored_stream": {
            "class": "logging.StreamHandler", 
            "formatter": "colored",
            "filters": ["event_blacklist"], # <- filters to use for this handler
        },

Source: https://docs.python.org/3/howto/logging-cookbook.html#custom-handling-of-levels

Do whatever you want with the filter.

@adrenaline681
Copy link
Author

This is what I ended up doing yesterday

DEV_FILTERED_EVENTS = ['request_started', 'request_finished', 'task_started', 'task_succeeded']

class DevelopmentFilter(logging.Filter):
    """ Filter out events in development so they don't clutter the console """
    def filter(self, record):
        if settings.DEBUG and type(record.msg) == dict:
            event = record.msg.get('event')
            if event in DEV_FILTERED_EVENTS:
                return False
        return True

@jrobichaud
Copy link
Owner

I was not sure you found a solution since you left the issue opened. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants