This is a sample Django application that demonstrates how to integrate Logtail for structured logging in a Django project.
- Structured logging with Logtail
- Different log levels (INFO, WARNING, ERROR)
- Custom context and extra data in logs
- Exception handling and logging
- Docker support
- Django 5.1.3 with Python 3.12
- Docker
- A Better Stack account
- Go to Better Stack -> Telemetry -> Sources, and create new Python source
- Clone this repository
- Copy
.env.exampleto.envand update the values:BETTER_STACK_SOURCE_TOKEN=your-source-token-here BETTER_STACK_INGESTING_HOST=your-source-ingesting-host-here
-
Build the Docker image:
docker build -t logtail-django-demo . -
Run the container:
docker run -p 8000:8000 --env-file .env logtail-django-demo
-
Visit http://localhost:8000 in your browser
The demo includes three endpoints that trigger different types of logs:
- Info Log (Homepage): Logs basic request information
- Warning Log (/trigger-warning/): Logs a warning with custom data
- Error Log (/trigger-error/): Triggers and logs an exception
Check your Logtail dashboard to see the logged events.
example-django/
├── demo/
│ ├── templates/
│ │ └── index.html
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── .env
├── Dockerfile
├── README.md
├── manage.py
└── requirements.txt
The logging configuration can be found in settings.py. It sets up both console and Logtail handlers with a verbose formatter.