Metadata-Version: 2.2
Name: loki-django-logger
Version: 1.0.7
Summary: Loki logger for Django applications
Home-page: https://github.com/irwinrex/django-loki-logger
Author: Irwin Rex
Author-email: irwinrex.a@gmail.com
Keywords: django loki logger async log-aggregation
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: System :: Logging
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=4.0
Requires-Dist: requests>=2.26.0
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# Loki Django Logger

This package provides a lightweight logging solution for Django applications that sends logs to Grafana Loki with gzip compression for improved performance.

## Installation

```bash
pip install loki-django-logger
```

## Configuration

1. Add the logger to your Django settings.

In your `settings.py`:

```python
from loki_django_logger.logger import configure_logger

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S"
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "loki": {
            "class": "loki_django_logger.handler.AsyncGzipLokiHandler",
            "loki_url": "https://loki.test.dev",
            "labels": {"application": "django-app", "environment": "development"},
            "level": "DEBUG",
            "flush_interval": 1,
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "loki"],
            "level": "INFO",
            "propagate": False,
            "extra": {}  # Add additional metadata here
        },
    },
}
```

2. Install Loki if not already available:

```bash
docker run -d --name=loki -p 3100:3100 grafana/loki:latest
```

3. Run your Django application and monitor the logs in Loki.

## Example Usage

In your Django views or tasks:

```python
import logging
logger = logging.getLogger("django")

def sample_view(request):
    logger.info("Sample log message sent to Loki", extra={"user_id": 123, "operation": "sample_view"})
    return JsonResponse({"message": "Logged successfully!"})
```

## Adding Extra Data to Logs

You can provide additional metadata to your logs using the `extra` parameter:

```python
logger.error("Failed to process request", extra={"user_id": 456, "error_code": "E500"})
```

## Testing

To run tests:

```bash
pytest tests/
```

## License

This project is licensed under the MIT License. See the `LICENSE` file for details.
