Skip to main content

No project description provided

Project description

Django Celery Logging with OpenObserve

This Django project provides middleware and utilities for logging request/response cycles, exceptions, and custom logs to an external logging system (OpenObserve) asynchronously using Celery.

Features

  • Request and Response Logging: Logs details of every request and response.
  • Exception Logging: Logs unhandled exceptions during requests.
  • Custom Logging: Allows developers to log custom information with different log levels.
  • Asynchronous Logging: Uses Celery to send logs asynchronously, ensuring no delay in the request/response cycle.

Requirements

  • Django
  • Celery
  • OpenObserve account (for external log tracking)

Installation

1. Install Package

Install the required dependencies with:

pip install open_observe_tracking_django

2. Configure Celery in Django

Create a celery.py file in the Django project directory (where settings.py is located):

Note: Change mysite.settings to your_django_project_name.settings

# mysite/mysite/celery.py


import os
from pathlib import Path
from celery import Celery
from django.conf import settings


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

    # SETUP CELERY 
    # Create the folder if not exists
    app = Celery( __name__)
    app.config_from_object('django.conf:settings',namespace='CELERY')

    if settings.BROKER_DATA_FOLDER:
      path = Path(settings.BROKER_DATA_FOLDER)
      if not path.exists():
          path.mkdir(parents=True, exist_ok=True)
    # Load tasks from all registered Django app configs.
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

Modify __init__.py in the same folder to import Celery:

# mysite/mysite/__init__.py

from .celery import app as celery_app

__all__ = ('celery_app',)

3. Configure Celery in settings.py

Add the following configurations to your settings.py:

# Celery Configuration
CELERY_BROKER_URL = 'redis://localhost:6379/0'  # Redis as the broker
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Or this if you want to use filesystem as brocker

# Celery Configuration
BROKER_DATA_FOLDER = './.data/broker'
BROKER_DATA_FOLDER_PATH = Path(BROKER_DATA_FOLDER)
if not BROKER_DATA_FOLDER_PATH.exists():
    BROKER_DATA_FOLDER_PATH.mkdir(parents=True, exist_ok=True)

CELERY_BROKER_URL = 'filesystem://'
CELERY_BROKER_TRANSPORT_OPTIONS = {
    'data_folder_in': BROKER_DATA_FOLDER,
    'data_folder_out':BROKER_DATA_FOLDER,
}

4. Set Up OpenObserve

Create a logging configuration for OpenObserve in the settings.py file:

Note: Set open_observe_stream_name as your app name, this will be the identifier in open observe

# Open observe
OPENOBSERVE_CONFIG = {
  "open_observe_host":"http(s)://open_observe_host:port",
  "open_observe_organization_name":'development-dev',
  "username":'your_user_name',
  "password":'password',
  "open_observe_stream_name":"app_name",
}

5. Create a Logging Middleware

The middleware automatically logs each request and response, as well as unhandled exceptions:

# Add OpenObserveTrackingMiddleware to your MIDDLEWARE list in settings.py
MIDDLEWARE = [
    'open_observe_tracking_django.OpenObserveTrackingMiddleware', # Put as the first one so all requests are logged
    # Other middleware...
]

6. Custom Logging

You can log custom information in your views using the log_to_open_observe function:

# In your views.py

from open_observe_tracking_django import log_to_open_observe, LogLevel

def my_view(request):
    info = {"user_action": "Login attempt", "username": "example_user"}
    log_to_open_observe(request, LogLevel.INFO, info)
    return HttpResponse("Logging Test")

8. Start Celery

Ensure your brocker is is running if other than filesystem is being used. You can start the Celery worker to handle asynchronous tasks using: (mysite is the name of the django project)

celery -A mysite worker --loglevel=info 

9. Running the Django Application

Run the Django application with:

python manage.py runserver

Usage

After setting up the project, logs from request/response cycles, exceptions, and custom logs will be sent to OpenObserve asynchronously using Celery.

You can monitor the logs on your OpenObserve dashboard.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

open_observe_tracking_django-0.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

open_observe_tracking_django-0.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file open_observe_tracking_django-0.2.tar.gz.

File metadata

File hashes

Hashes for open_observe_tracking_django-0.2.tar.gz
Algorithm Hash digest
SHA256 b01699a25fbc2e9ed28f4e8fbc2a597c5cb4c85c4fa1689eed3ebfb78aa7923f
MD5 8e7fa876518f6db767728e334bcb044d
BLAKE2b-256 90d945fe5488cbe9dac7fb8b9d357ed93fcdaa3713a518c1decde411fac9de2e

See more details on using hashes here.

File details

Details for the file open_observe_tracking_django-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for open_observe_tracking_django-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4c252c55045c7d559888f64eaf666b8365a84cc171d0a634f520380bc4a847f9
MD5 898daac13a5a9c89ed3cd3be872447db
BLAKE2b-256 360d0621dd120c1aa9de7df0857717d1f30c915c54efcec7ca9cafd17d74c72e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page