Official Python SDK for Logwell logging platform
Project description
Logwell Python SDK
Official Python SDK for the Logwell logging platform.
Installation
pip install logwell
Quick Start
import asyncio
from logwell import Logwell
# Initialize client
client = Logwell({
'api_key': 'lw_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'endpoint': 'https://logs.example.com',
'service': 'my-app',
})
# Log messages at different levels
client.debug('Debug message')
client.info('User logged in', {'user_id': '123'})
client.warn('Disk space low', {'available_gb': 5})
client.error('Failed to process request', {'request_id': 'abc'})
client.fatal('Database connection lost')
# Ensure logs are sent before exit
asyncio.run(client.shutdown())
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
api_key |
str |
required | API key in format lw_[32 chars] |
endpoint |
str |
required | Logwell server URL |
service |
str |
None |
Default service name for all logs |
batch_size |
int |
50 |
Number of logs to batch before auto-flush |
flush_interval |
float |
5.0 |
Seconds between auto-flushes |
max_queue_size |
int |
1000 |
Maximum queue size before dropping oldest |
max_retries |
int |
3 |
Retry attempts for failed requests |
capture_source_location |
bool |
False |
Capture file/line info |
on_error |
Callable |
None |
Error callback |
on_flush |
Callable |
None |
Flush callback |
Example with all options
from logwell import Logwell
def on_error(err):
print(f'Logging error: {err}')
def on_flush(count):
print(f'Flushed {count} logs')
client = Logwell({
'api_key': 'lw_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'endpoint': 'https://logs.example.com',
'service': 'my-app',
'batch_size': 100,
'flush_interval': 10.0,
'max_queue_size': 5000,
'max_retries': 5,
'capture_source_location': True,
'on_error': on_error,
'on_flush': on_flush,
})
API Reference
Logwell
The main client class.
Constructor
Logwell(config: LogwellConfig)
Methods
| Method | Description |
|---|---|
debug(message, metadata=None) |
Log at debug level |
info(message, metadata=None) |
Log at info level |
warn(message, metadata=None) |
Log at warning level |
error(message, metadata=None) |
Log at error level |
fatal(message, metadata=None) |
Log at fatal level |
log(entry) |
Log with explicit LogEntry |
flush() |
Async: Flush queued logs immediately |
shutdown() |
Async: Flush and stop the client |
child(metadata=None, service=None) |
Create child logger with context |
queue_size |
Property: Current queue size |
Child Loggers
Create child loggers to add persistent context:
# Create child logger with request context
request_logger = client.child({'request_id': 'abc-123'})
request_logger.info('Processing request') # Includes request_id
# Override service name
db_logger = client.child(service='my-app-db')
db_logger.info('Query executed', {'duration_ms': 45})
Log Entry
from logwell import LogLevel
# Using log() with explicit entry
client.log({
'level': 'info',
'message': 'Custom log',
'metadata': {'key': 'value'},
'service': 'override-service',
'timestamp': '2024-01-01T00:00:00Z', # Optional, auto-generated
})
LogLevel
Available log levels: debug, info, warn, error, fatal
LogwellConfig
TypedDict with configuration options. See Configuration section above.
IngestResponse
Response from the server after flushing logs:
{
'accepted': 50, # Logs accepted
'rejected': 0, # Logs rejected (optional)
'errors': [], # Error messages (optional)
}
Error Handling
LogwellError
All SDK errors are wrapped in LogwellError:
from logwell import Logwell, LogwellError, LogwellErrorCode
try:
client = Logwell({'api_key': 'invalid', 'endpoint': 'https://example.com'})
except LogwellError as e:
print(e.message) # Human-readable message
print(e.code) # LogwellErrorCode enum
print(e.status_code) # HTTP status (if applicable)
print(e.retryable) # Whether operation can be retried
Error Codes
| Code | Description |
|---|---|
INVALID_CONFIG |
Invalid configuration value |
NETWORK_ERROR |
Network connectivity or timeout |
UNAUTHORIZED |
Invalid or expired API key (401) |
VALIDATION_ERROR |
Invalid request data |
RATE_LIMITED |
Too many requests (429) |
SERVER_ERROR |
Server-side error (5xx) |
QUEUE_OVERFLOW |
Queue exceeded max size |
Error Callback
Handle errors without try/catch:
def handle_error(err: Exception):
if isinstance(err, LogwellError):
if err.code == LogwellErrorCode.NETWORK_ERROR:
print('Network issue, logs will be retried')
elif err.code == LogwellErrorCode.QUEUE_OVERFLOW:
print('Queue full, some logs dropped')
client = Logwell({
'api_key': 'lw_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'endpoint': 'https://logs.example.com',
'on_error': handle_error,
})
Async Usage
The SDK uses async for flush and shutdown operations:
import asyncio
from logwell import Logwell
async def main():
client = Logwell({
'api_key': 'lw_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'endpoint': 'https://logs.example.com',
})
client.info('Starting app')
# Manual flush
response = await client.flush()
print(f'Sent {response["accepted"]} logs')
# Shutdown gracefully
await client.shutdown()
asyncio.run(main())
Source Location Capture
Enable automatic file/line capture:
client = Logwell({
'api_key': 'lw_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'endpoint': 'https://logs.example.com',
'capture_source_location': True,
})
client.info('This log includes file and line number')
# Log includes: source_file='app.py', line_number=42
Requirements
- Python 3.9+
- httpx >= 0.25.0
License
MIT License - see LICENSE for details.
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 logwell-1.0.1.tar.gz.
File metadata
- Download URL: logwell-1.0.1.tar.gz
- Upload date:
- Size: 92.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
674e408569347ca6ceb91b1e8f6edde83cec4d41648b9cd92a0205f4f08536e3
|
|
| MD5 |
379abbbade9083089c3c3d7ef6d2d991
|
|
| BLAKE2b-256 |
33363f8a9c1e95253f7798f304f7d3d663f78d75434502a103c397f1e8a540b2
|
Provenance
The following attestation bundles were made for logwell-1.0.1.tar.gz:
Publisher:
sdk-python.yml on Divkix/Logwell
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logwell-1.0.1.tar.gz -
Subject digest:
674e408569347ca6ceb91b1e8f6edde83cec4d41648b9cd92a0205f4f08536e3 - Sigstore transparency entry: 1282836232
- Sigstore integration time:
-
Permalink:
Divkix/Logwell@6e43076fa174781b4fbe9046ba9ee96c17d73bd2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Divkix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
sdk-python.yml@6e43076fa174781b4fbe9046ba9ee96c17d73bd2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file logwell-1.0.1-py3-none-any.whl.
File metadata
- Download URL: logwell-1.0.1-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb3efc18b5496c5ec5d27dc68fb5bbbe45f9c7d95012a5109bae7dd0ac4459dd
|
|
| MD5 |
e5331aea0263a562878e65560b11b930
|
|
| BLAKE2b-256 |
02d7fc85bf251426572e59ea58214c3977996115e120b721445075800fc0fdaa
|
Provenance
The following attestation bundles were made for logwell-1.0.1-py3-none-any.whl:
Publisher:
sdk-python.yml on Divkix/Logwell
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logwell-1.0.1-py3-none-any.whl -
Subject digest:
cb3efc18b5496c5ec5d27dc68fb5bbbe45f9c7d95012a5109bae7dd0ac4459dd - Sigstore transparency entry: 1282836238
- Sigstore integration time:
-
Permalink:
Divkix/Logwell@6e43076fa174781b4fbe9046ba9ee96c17d73bd2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Divkix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
sdk-python.yml@6e43076fa174781b4fbe9046ba9ee96c17d73bd2 -
Trigger Event:
push
-
Statement type: