Modern Python uygulamaları için log yönetim ve hata izleme kütüphanesi
Project description
Loggier
Loggier, Python tabanlı backend uygulamaları için geliştirilen, düşük maliyetli bir log yönetim sistemidir. Bu kütüphane, hataları ve logları yakalayıp işleyerek merkezi bir sunucuya göndermeyi sağlar.
Kurulum
pip install loggier
Hızlı Başlangıç
from loggier import Loggier
# Loggier'ı başlat
logger = Loggier(api_key="your_api_key_here")
# Temel log kayıtları
logger.info("Bu bir bilgi mesajıdır")
logger.warning("Bu bir uyarı mesajıdır")
logger.error("Bu bir hata mesajıdır")
# Bağlam ile log
logger.info("Kullanıcı oturum açtı", context={"user_id": 123, "username": "testuser"})
# Hata takibi
try:
1 / 0
except Exception as e:
logger.exception("Bir hata oluştu", exception=e)
# Context manager kullanımı
with logger.context(transaction="ödeme işlemi", user_id=123):
# Bu blok içindeki tüm loglara bağlam bilgisi eklenir
logger.info("Ödeme işlemi başlatıldı")
logger.info("Ödeme işlemi tamamlandı")
Özellikler
- Esnek Log Seviyeleri: info, warning, error, critical
- Asenkron Gönderim: Uygulamanızı yavaşlatmadan log gönderimi
- Otomatik Bağlam Toplama: Çalışma zamanı bilgileri ve ortam verileri
- Hata İzleme: Otomatik istisna ve izleme yakalama
- Özel Etiketler ve Metadata: Loglarınızı özelleştirme
- Otomatik Yeniden Deneme: Bağlantı sorunlarında güvenilir log gönderimi
- Web Framework Entegrasyonları: Django, Flask, FastAPI için hazır entegrasyonlar
Gelişmiş Kullanım
Web Framework Entegrasyonları
Django Integration Guide for Loggier
This guide explains how to integrate Loggier with your Django project for request/response logging and error tracking.
Installation
First, ensure you have installed the Loggier package:
pip install loggier
Integration Options
Option 1: Direct Middleware (Recommended)
The simplest way to integrate Loggier with your Django project is to add LoggierDjangoMiddleware directly to your MIDDLEWARE setting in settings.py:
# settings.py
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware', # If you use corsheaders
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# ... other middleware
'loggier.integrations.django.LoggierDjangoMiddleware', # Add Loggier middleware
# ... other middleware
]
# Configure Loggier
LOGGIER = {
'API_KEY': 'your-api-key-here',
'API_URL': 'http://localhost:8000/', # Or your Loggier server URL
'ENVIRONMENT': 'development', # or 'production', 'staging', etc.
'TAGS': ['django', 'web'],
'CONTEXT': {
'application': 'your-app-name'
},
'CAPTURE_REQUEST_DATA': True,
'LOG_SLOW_REQUESTS': True,
'SLOW_REQUEST_THRESHOLD': 1.0 # in seconds
}
Option 2: Custom Middleware Module (Legacy Approach)
If you need more control over the middleware initialization, you can create a custom middleware module:
# myapp/middleware.py
from loggier.integrations.django import LoggierDjango
# Create instance
loggier_django = LoggierDjango(
api_url='http://localhost:8000/',
api_key='your-api-key-here',
environment='development',
tags=['django', 'web'],
# Other parameters...
)
# Get the middleware class
LoggierMiddleware = loggier_django.get_middleware()
Then in your settings.py:
MIDDLEWARE = [
# ... other middleware
'myapp.middleware.LoggierMiddleware',
# ... other middleware
]
Configuration Options
The LOGGIER settings dictionary supports the following options:
| Option | Description | Default |
|---|---|---|
API_KEY |
Your Loggier API key | Required |
API_URL |
Loggier API endpoint | https://api.loggier.com/api/ingest |
ENVIRONMENT |
Environment name (production, staging, etc.) | development |
ASYNC_MODE |
Whether to send logs asynchronously | True |
MAX_BATCH_SIZE |
Maximum batch size for async mode | 100 |
FLUSH_INTERVAL |
Flush interval in seconds for async mode | 5 |
TAGS |
List of tags to add to all logs | [] |
CONTEXT |
Dictionary of context data to add to all logs | {} |
LOG_LEVEL |
Minimum log level to send | info |
CAPTURE_REQUEST_DATA |
Whether to capture request data | True |
LOG_SLOW_REQUESTS |
Whether to log slow requests | True |
SLOW_REQUEST_THRESHOLD |
Threshold for slow requests in seconds | 1.0 |
What Gets Logged
With the default configuration, Loggier will automatically log:
- Request Start: Basic information about each incoming request
- Request End: Response status, timing, and size
- Exceptions: Detailed exception information with traceback
- Slow Requests: Warning logs for requests that exceed the threshold
Manual Logging
You can also use the Loggier client directly in your views:
from loggier import Loggier
logger = Loggier(
api_key='your-api-key',
api_url='http://localhost:8000/',
environment='development'
)
def my_view(request):
# Log information
logger.info("Processing user data", context={"user_id": request.user.id})
try:
# Your code here
result = process_data()
logger.info("Data processed successfully", context={"result_size": len(result)})
except Exception as e:
# Log exceptions
logger.exception("Error processing data", exception=e)
raise
return HttpResponse("Success")
Troubleshooting
Parameter Errors
If you encounter errors like TypeError: __init__() got an unexpected keyword argument 'context', it means your installed version of Loggier doesn't match the expected API. Check your installed version with:
pip show loggier
And either:
- Update to the latest version:
pip install --upgrade loggier - Modify your middleware to match the API of your installed version
Middleware Import Errors
Make sure the middleware path in your MIDDLEWARE setting exactly matches where you've defined the middleware class. If using the legacy approach, ensure you're referencing LoggierMiddleware (not LoggierDjango):
# CORRECT:
'myapp.middleware.LoggierMiddleware', # Notice it's LoggierMiddleware, not LoggierDjango
# INCORRECT:
'myapp.middleware.LoggierDjango', # This won't work
Performance Considerations
- Loggier uses asynchronous logging by default, which minimizes performance impact
- For high-traffic applications, consider adjusting the
MAX_BATCH_SIZEandFLUSH_INTERVAL - In production environments, make sure to call
flush()during application shutdown to ensure all logs are sent
Project details
Release history Release notifications | RSS feed
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 loggier-0.1.1.tar.gz.
File metadata
- Download URL: loggier-0.1.1.tar.gz
- Upload date:
- Size: 39.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
484c14be1e6aba7b372700d7e3b628a82950e5036d3cb97b5dced91fbc40ad20
|
|
| MD5 |
f126baf25d141c332110297e36bb4587
|
|
| BLAKE2b-256 |
1338cd8ab6cf95553ae46b629bb3d059a78d3d35789022cb5b28c19a6abd8959
|
File details
Details for the file loggier-0.1.1-py3-none-any.whl.
File metadata
- Download URL: loggier-0.1.1-py3-none-any.whl
- Upload date:
- Size: 44.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53989b6e147af3e2d5f8dd78481341a0165cf161c1c2f2eba7b4d630a0d13601
|
|
| MD5 |
a60babfdc46b1ab75db39bd184b378ec
|
|
| BLAKE2b-256 |
bb7cd46ebe5e5fa68ba42b467a68b51fa5427c3852f71406619445481a6de07e
|