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

🔊 add celery logging #87

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

🔊 add celery logging #87

wants to merge 3 commits into from

Conversation

Coperh
Copy link
Contributor

@Coperh Coperh commented Nov 18, 2024

Partially fixes: maykinmedia/objects-api#470
Partially fixes: open-zaak/open-zaak#1802

Needs OAF release

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.76%. Comparing base (0939eb4) to head (476762c).
Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #87      +/-   ##
==========================================
+ Coverage   85.62%   92.76%   +7.14%     
==========================================
  Files           8       13       +5     
  Lines         153      304     +151     
==========================================
+ Hits          131      282     +151     
  Misses         22       22              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Coperh
Copy link
Contributor Author

Coperh commented Nov 18, 2024

There is currently a LOG_LEVEL variable that affects console and django log output and a CELERY_LOGLEVEL that is used the celery commands

If we used CELERY_LOGLEVEL for the celery logger, it will only display at the level of LOG_LEVEL. So is celery is DEBUG and the main level is INFO, it will only log celery INFO.

I think there are two ways to handle this:

  • Remove CELERY_LOGLEVEL and just use LOG_LEVEL for everything
  • Create separate console and file logs for celery and keep its own level variable.

@Coperh
Copy link
Contributor Author

Coperh commented Nov 18, 2024

@annashamray do you think it is fine to CELERY_LOGLEVEL? Or should I create a separate logging for celery?

@annashamray
Copy link
Contributor

I don't mind your solution of removing CELERY_LOGLEVEL
But I'm not sure if devops actually use the separate env var for celery logging

@stevenbal What do you thing?

@stevenbal
Copy link
Contributor

@annashamray might be worth asking Sjoerd, but I'd also be fine with removing CELERY_LOGLEVEL and relying on the other variable LOG_LEVEL

@Coperh
Copy link
Contributor Author

Coperh commented Nov 22, 2024

@stevenbal I did ask Sjoerd and he would like both like open-forms.

[They] are used to setting CELERY_LOGLEVEL to debug and just leave LOG_LEVEL on its default

This does require creating separate loggers for CELERY.

@stevenbal
Copy link
Contributor

if it's easy to make it consistent with OF, then let's do that. Though I don't see a specific logger for celery in OF?

@Coperh Coperh force-pushed the bug/fix-celery-logging branch from 1ec0fa4 to ce1ff80 Compare November 22, 2024 12:04
@Coperh
Copy link
Contributor Author

Coperh commented Nov 22, 2024

Added the new loggers for console and file.

The way its currently implemented in projects:

  • Open Forms celery does not use the Django logger at all.
  • OAF uses the Django logger but celery was missing from the loggers.
  • Open Inwoner does use Django logger with celery setup but does not have a LOG_LEVEL var and always logs DEBUG to the base handlers. Logs from celery based on a CELERY_LOGLEVEL envvar.

My current approach is to add back the CELERY_LOGLEVEL envvar and create separate celerly loggers, but we can also use the open-forms approach. It depends of if the Django console is used for debugging celery

@Coperh
Copy link
Contributor Author

Coperh commented Nov 27, 2024

@annashamray @stevenbal can you re-review the new changes and my above comment? Thanks

Copy link
Contributor

@annashamray annashamray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the research
I have no strong opinion how to configure Celery logs (probably because I don't do devops tasks), so for me all options look fine.
Your approach with logging to the separate file is good!

@@ -507,6 +527,11 @@
"level": "DEBUG",
"propagate": True,
},
"celery": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this will add the logs of the celery library itself, does the logging that happens inside celery tasks still work as well? Because from Sjoerd's issue I see that there is no logging at all. The regular logging is picked up by the project handler i think, even if it happens inside the celery worker

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does appearing in Celery yes

Screenshot from 2024-11-29 15-17-21

Also with just INFO

Screenshot from 2024-11-29 15-16-35

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does need LOG_STDOUT to be enabled though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check, I think it's fine to accept these changes then. I do think that the logs no longer showing has to do with either LOG_STDOUT or LOG_LEVEL, so I've asked Sjoerd to see what the values of those are open-zaak/open-zaak#1802 (comment)

@Coperh
Copy link
Contributor Author

Coperh commented Dec 2, 2024

Also note, with these changes, CELERY ignores setting the log level on the command line.

edit: this can be fixed in config_loggers in celery.py in all the projects.

Perhaps there should be a common celery.py or celery functions in utils?

@@ -415,6 +422,19 @@
"class": "logging.StreamHandler",
"formatter": "db",
},
"console_celery": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note to rename one or the other

@Coperh
Copy link
Contributor Author

Coperh commented Dec 2, 2024

Example of above

@setup_logging.connect()
def config_loggers(loglevel, logfile, format, colorize, **kwargs):
    from logging.config import dictConfig

    if logfile:
        settings.LOGGING["handlers"]["celery_file"]["filename"] = logfile
    settings.LOGGING["formatters"]["celery"]["format"] = format
    settings.LOGGING["loggers"]["celery"]["level"] = loglevel
    dictConfig(settings.LOGGING)

Really depends on what we want to allow.

Also, should this change the LOG_STDOUT?

@alextreme
Copy link
Member

Discussed with Conor, and as the default was that there was output on stdout lets set LOG_STDOUT to True by default. In Docker and K8s installs you want logging on stdout and that is our default way of deployment, if someone doesn't want this they can turn it off via this envvar

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

Successfully merging this pull request may close these issues.

CELERY_LOGLEVEL: DEBUG does not work No celery worker debug logs
5 participants