import os
from logging.config import dictConfig
from celery import Celery
from celery.signals import setup_logging
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.local")
from django.conf import settings
app = Celery("app")
app.config_from_object("django.conf:settings", namespace="CELERY")
@setup_logging.connect
def config_loggers(*args, **kwargs):
"""Hook to pass django logging config to celery"""
dictConfig(settings.LOGGING)
app.conf.result_expires = 60 * 60
app.conf.update(result_extended=True)
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
The handler is implemented like that:
# logging
import logging
from logging_journald import JournaldLogHandler, check_journal_stream
# exceptions
from django.core.exceptions import ImproperlyConfigured
class CustomJournaldHandler(JournaldLogHandler):
"""Logging handler for journald."""
def __init__(self, *args, use_message_id: bool = False, **kwargs):
if not (
# Check if program running as systemd service
check_journal_stream()
or
# Check if journald socket is available
JournaldLogHandler.SOCKET_PATH.exists()
):
raise ImproperlyConfigured(
"Neither running as systemd service nor journald socket available."
)
super().__init__(*args, use_message_id=use_message_id, **kwargs)
The message Unable to write message 'INFO 2024-11-30 12:38:51,523 beat: Starting...' to journald results from this error:
Traceback (most recent call last):
File "/home/***/.venv/lib/python3.11/site-packages/logging_journald.py", line 126, in send
self.socket.sendall(value)
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/***/.venv/lib/python3.11/site-packages/logging_journald.py", line 265, in emit
self.transport.send(self._format_record(record))
File "/home/***/.venv/lib/python3.11/site-packages/logging_journald.py", line 134, in send
self.socket.sendmsg(
OSError: [Errno 9] Bad file descriptor
Also it seams to be that celery beat is the problem here, because starting celery without beat: celery -A settings worker --scheduler django -O fair -l DEBUG -E does nor result in the Unable to write message.
I am using Django, Celery, Celery Beat and logging-journald.
In Django I have defined the journald logging handler like that:
Other Django settings:
Celery.py:
The handler is implemented like that:
When starting Celery and Celery Beat with:
celery -A settings worker --beat --scheduler django -O fair -l DEBUG -E
I get the following log:
The message
Unable to write message 'INFO 2024-11-30 12:38:51,523 beat: Starting...' to journald
results from this error: