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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file open_observe_tracking_django-0.2.1.tar.gz.
File metadata
- Download URL: open_observe_tracking_django-0.2.1.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab90c30cf3c43d0840ec6a6f127df6558efa9a1055be49a898e3a0fba33be2d4
|
|
| MD5 |
98b48eab5a7883337e3741cd739d552d
|
|
| BLAKE2b-256 |
8f130b2c657cd81639c9b86824e4119980947c9cef955e2bcb273f8e2c5f873c
|
File details
Details for the file open_observe_tracking_django-0.2.1-py3-none-any.whl.
File metadata
- Download URL: open_observe_tracking_django-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac8df59e77cfc3d28cd32407ca5f06ab8766a475fa3e7ac9a9289fc50e4b7ec5
|
|
| MD5 |
a9f029894e089793ad506e1426ddea8f
|
|
| BLAKE2b-256 |
80d12455f0e7569eb6d14d48bc0700e1296de89f8f3031579a25ef04183bec56
|