GreyZmeem / python-logging-loki

Python logging handler for Loki
MIT License
151 stars 57 forks source link

Issues when using with Celery #24

Open pesfahanian opened 2 years ago

pesfahanian commented 2 years ago

I have the following method that runs as a background task:

import logging

from config.celery import app
from config.queues import my_queue

logger = logging.getLogger(__name__)

@app.task(name='my-method', queue=my_queue)
def my_method(**kwargs) -> None:
    # Method body ...
    logger.info('This is my log message!', extra={
        'tags': {
            'foo': 'bar',
        },
    })

Calling this method anywhere as:

my_method.delay(**kwargs)

will result Celery disconnecting from the broker (which is RabbitMQ) with the following WARNING message:

rabbitmq       | 2022-05-23 06:51:06.177726+00:00 [warning] <0.1438.0> closing AMQP connection <0.1438.0> (172.21.0.1:38034 -> 172.21.0.4:5672, vhost: '/', user: 'guest'):
rabbitmq       | 2022-05-23 06:51:06.177726+00:00 [warning] <0.1438.0> client unexpectedly closed TCP connection

Furthermore, the message is also not sent to Loki. Here is my Loki settings:

LOKI_SETTINGS = {
    'level': 'INFO',
    'class': 'logging_loki.LokiHandler',
    'url': LOKI_URL,
    'tags': {
        'app': 'my-app'
    },
    'auth': (LOKI_USER, LOKI_PASSWORD),
    'version': '1',
}

And here is my Django logging setting:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
        'loki': LOKI_SETTINGS,
    },
    'loggers': {
        '': {
            'handlers': ['console', 'loki'],
            'level': 'DEBUG',
            'propagate': True,
        }
    }
}

I'm using Django==3.2, celery==5.1.2, and python-logging-loki==0.3.1. My RabbitMQ is also version 3.9.16.

Thanks!