Datares Logger Python SDK
Official Python SDK for Datares Logger — structured log ingestion by Datares.
Requirements: Python 3.9+
Installation
pip install datares-logger
Quick Start
from datares_logger import Logger
logger = Logger("dtr_live_<your-api-key>")
logger.info("Application started", service="api")
logger.warning("Disk usage high", service="api", meta={"usage_pct": 92})
logger.error("Payment failed", service="billing", meta={"order_id": "ord_123"})
# The buffer is flushed automatically when the process exits.
# Call flush() explicitly if you need to ensure delivery before that point.
logger.flush()
Logger
Constructor
Logger(api_key: str, config: Config = Config())
| Parameter | Type | Description |
|---|---|---|
api_key |
str |
Datares API key (dtr_live_<48 hex chars>) |
config |
Config |
Optional SDK configuration (see below) |
Convenience Methods
All convenience methods accept the same arguments:
logger.debug (message, service="default", meta={})
logger.info (message, service="default", meta={})
logger.warning (message, service="default", meta={})
logger.error (message, service="default", meta={})
logger.critical(message, service="default", meta={})
| Parameter | Type | Description |
|---|---|---|
message |
str |
Human-readable log message (required) |
service |
str |
Service / component name (default: "default") |
meta |
dict |
Arbitrary key-value metadata |
These methods add the entry to the internal buffer. The buffer is flushed
automatically when it reaches config.batch_size or when the process exits.
log(entry: LogEntry)
Add a pre-built LogEntry to the buffer.
from datares_logger import Logger, LogEntry
logger = Logger("dtr_live_...")
entry = LogEntry(level="info", message="Custom entry", service="worker")
logger.log(entry)
send(entries) -> int
Send one or more entries immediately, bypassing the buffer. Raises on any failure (does not call the error handler).
count = logger.send(LogEntry(level="error", message="Critical alert"))
count = logger.send([entry1, entry2, entry3])
Returns the number of entries accepted by the server.
flush()
Flush all buffered entries to the API. On failure the registered error handler is called (default: print to stderr). The buffer is always cleared, regardless of success or failure.
logger.flush()
on_error(handler) -> Logger
Register a callback invoked when flush() fails.
Returns self for method chaining.
import sentry_sdk
logger.on_error(lambda err: sentry_sdk.capture_exception(err))
LogEntry
from datares_logger import LogEntry
from datetime import datetime, timezone
entry = LogEntry(
level="warning",
message="Something went wrong",
service="payments",
timestamp=datetime(2024, 6, 15, 12, 0, 0, tzinfo=timezone.utc),
meta={"order_id": "ord_456", "amount": 9900},
)
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
level |
str |
Yes | — | Log level string (see Level enum) |
message |
str |
Yes | — | Log message |
service |
str |
No | "default" |
Service / component name |
timestamp |
datetime or None |
No | current time | Event time (RFC 3339 when serialised) |
meta |
dict |
No | {} |
Arbitrary key-value metadata |
Level
from datares_logger import Level
Level.DEBUG # "debug"
Level.INFO # "info"
Level.WARNING # "warning"
Level.ERROR # "error"
Level.CRITICAL # "critical"
Config
from datares_logger import Config, Logger
config = Config(
base_url="https://api.datares.id",
timeout=10,
retries=3,
batch_size=100,
auto_flush=True,
)
logger = Logger("dtr_live_...", config=config)
| Option | Type | Default | Description |
|---|---|---|---|
base_url |
str |
"https://api.datares.id" |
API base URL (no trailing slash) |
timeout |
int |
10 |
HTTP request timeout in seconds |
retries |
int |
3 |
Retry attempts on 5xx errors (exponential back-off) |
batch_size |
int |
100 |
Auto-flush buffer when this many entries are buffered (1–500) |
auto_flush |
bool |
True |
Register atexit handler to flush on process exit |
Error Handling
from datares_logger import Logger, AuthError, RateLimitError, ApiError, LoggerError
logger = Logger("dtr_live_...")
# send() raises — handle explicitly:
try:
logger.send(entry)
except AuthError:
print("Invalid API key")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after}s")
except ApiError as e:
print(f"API error: {e} (HTTP {e.status_code})")
# flush() uses the error handler:
logger.on_error(lambda err: print(f"Flush failed: {err}"))
logger.flush()
Exception Hierarchy
LoggerError
├── AuthError — HTTP 401 / 403 (invalid or missing API key)
├── RateLimitError — HTTP 429 (.retry_after: Optional[int])
└── ApiError — HTTP 422, 5xx after retries (.status_code: int)
Framework Integration
Django
In settings.py or your app AppConfig.ready():
from datares_logger import Logger, Config
datares = Logger(
api_key="dtr_live_...",
config=Config(batch_size=50),
)
In views / signals:
datares.info("User signed in", service="accounts", meta={"user_id": request.user.id})
Flask
from flask import Flask, g
from datares_logger import Logger
app = Flask(__name__)
datares = Logger("dtr_live_...")
@app.before_request
def log_request():
datares.info("Incoming request", service="web", meta={"path": request.path})
Standalone Script
from datares_logger import Logger
logger = Logger("dtr_live_...")
def main():
logger.info("Script started", service="cronjob")
# ... do work ...
logger.info("Script finished", service="cronjob")
# flush() is called automatically by atexit
if __name__ == "__main__":
main()
Running Tests
pip install -e ".[dev]"
pytest
# With JUnit XML report:
pytest --junit-xml=junit.xml
License
MIT — see LICENSE.
Metadata
Release files for datares-logger 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| datares_logger-0.1.0.tar.gz | 12.3 kB | Without provenance | Details for datares_logger-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| datares_logger-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for datares_logger-0.1.0-py3-none-any.whl |
Total release size: 24.0 kB
Release files / datares_logger-0.1.0.tar.gz
| Download URL | datares_logger-0.1.0.tar.gz |
|---|---|
| Size | 12.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
36030c6c22917f6dd32606de49820dc3eb14ff0d11651b6f4f182553224ac6da
|
|
BLAKE2b-256 checksum How to use checksums |
428fd5aa8b1d872b09f47a7bdb5a2108c094109abfcdbc646fc3816c1616e6f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.13
|
Release files / datares_logger-0.1.0-py3-none-any.whl
| Download URL | datares_logger-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
69086e4a41dda496a646d8772e7d511b693083e5d34ad729c2718e7b02958265
|
|
BLAKE2b-256 checksum How to use checksums |
3d231bc52b2e0e08b582d6f5ccf4fb133170e71d9f56eb7a5e380c766e540408
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.13
|
Security for datares-logger 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.