Skip to content

Commit

Permalink
Added logging for celery
Browse files Browse the repository at this point in the history
  • Loading branch information
sandstromviktor committed Feb 5, 2024
1 parent c194f34 commit c820eff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ kubernetes==28.1.0

# Logging
colorlog==6.8.2
django-structlog==7.1.0
django-structlog[celery]==7.1.0

# Building with global-options takes a very long time
#Pillow==9.4.0 --global-option="build_ext" --global-option="--disable-tiff" --global-option="--disable-freetype" --global-option="--disable-lcms" --global-option="--disable-webp" --global-option="--disable-webpmux" --global-option="--disable-imagequant" --global-option="--disable-xcb" --global-option="--disable-zlib"
Expand Down
14 changes: 13 additions & 1 deletion studio/celery.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from __future__ import absolute_import, unicode_literals

import os
from logging.config import dictConfig # noqa

from celery import Celery
from celery.signals import setup_logging
from django.conf import settings
from django_structlog.celery.steps import DjangoStructLogInitStep

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "studio.settings")

app = Celery("studio")

app.steps["worker"].add(DjangoStructLogInitStep)
app.config_from_object("django.conf:settings", namespace="CELERY")


@setup_logging.connect
def config_loggers(*args, **kwargs):
# noqa
logger_config = settings.LOGGING
dictConfig(logger_config)


app.autodiscover_tasks()
13 changes: 10 additions & 3 deletions studio/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
]

STRUCTLOG_MIDDLEWARE = ["django_structlog.middlewares.RequestMiddleware"]

DJANGO_STRUCTLOG_CELERY_ENABLED = not DEBUG
# Application definition

INSTALLED_APPS = [
Expand Down Expand Up @@ -401,7 +401,6 @@
# Also anonymous access to pages was not working.
ANONYMOUS_USER_NAME = None


LOGGING = {
"version": 1,
"disable_existing_loggers": False,
Expand Down Expand Up @@ -436,7 +435,7 @@
"loggers": {
"": {
"handlers": ["console" if DEBUG else "json"],
"level": "DEBUG" if DEBUG else "INFO",
"level": "WARNING" if DEBUG else "INFO",
},
"django.server": {
"handlers": ["console"],
Expand All @@ -445,6 +444,14 @@
},
},
}
# Add logger for each installed app
for apps in INSTALLED_APPS:
LOGGING["loggers"][apps] = {
"handlers": ["console" if DEBUG else "json"],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
}

if not DEBUG:
structlog.configure(
processors=[
Expand Down

0 comments on commit c820eff

Please sign in to comment.