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 (when using the library in a django project)

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 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.

Modify the library

Edit the code, build the library with

python setup.py sdist bdist_wheel

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.2.tar.gz (7.9 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.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for open_observe_tracking_django-0.2.2.tar.gz
Algorithm Hash digest
SHA256 04ef4cdf11bc412cbe320f5077cfc12e3db5ff877cca4351292752007c3d5fe4
MD5 679813dd57442769d34c38b7443c8baf
BLAKE2b-256 33b8716dd9e9a56754932b9e86ab3c5a3a94cdb704806aa4fc530813b2f5faf5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for open_observe_tracking_django-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 27099c54b23e556974865898f21a6bbdd5924e316a202a92cb1f81b28e2acd42
MD5 9a3e215ef191b90dc21756b0bd3a3712
BLAKE2b-256 1670fc74319e07d4667e43af743a62e1c8455478a9ee3e2d297f5c5a6343dc88

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