Official Timberlogs SDK for Python - structured logging made simple
Project description
Timberlogs Python SDK
The official Timberlogs SDK for Python applications. Send structured logs to Timberlogs with automatic batching, retry logic, and flow tracking.
Installation
pip install timberlogs-client
Quick Start
from timberlogs import create_timberlogs
timber = create_timberlogs(
source="my-app",
environment="production",
api_key="tb_live_xxxxxxxxxxxxx",
)
timber.info("Hello, Timberlogs!")
Features
- Structured logging - Log with arbitrary JSON data
- Automatic batching - Efficiently batch logs before sending
- Retry with backoff - Automatic retry on failures
- Flow tracking - Group related logs together
- Level filtering - Filter logs by severity
- Async support - Full async/await support
- Context manager - Automatic cleanup with
withstatement
Basic Usage
Log Levels
timber.debug("Detailed diagnostic info", {"key": "value"})
timber.info("General information", {"user_id": "123"})
timber.warn("Warning condition", {"threshold": 90})
timber.error("Error occurred", {"code": "E001"})
Logging with Data
timber.info("User signed in", {
"user_id": "user_123",
"method": "oauth",
"provider": "google",
})
Error Logging
# With Exception object (extracts name, message, stack)
try:
risky_operation()
except Exception as e:
timber.error("Operation failed", e)
# With data object
timber.error("Validation failed", {
"field": "email",
"reason": "invalid format",
})
Tags
from timberlogs import LogOptions
timber.info("Payment processed", {"amount": 99.99}, LogOptions(tags=["payments", "success"]))
Setting User/Session IDs
# Set defaults for all subsequent logs
timber.set_user_id("user_123")
timber.set_session_id("sess_abc")
# Clear on logout
timber.set_user_id(None)
timber.set_session_id(None)
Method Chaining
timber.set_user_id("user_123").set_session_id("sess_abc").info("User action")
Flow Tracking
Flows group related logs together with automatic step indexing:
# Synchronous flow (local ID generation)
flow = timber.flow("checkout")
flow.info("Started checkout", {"items": 3})
flow.info("Payment processed", {"amount": 99.99})
flow.info("Order confirmed", {"order_id": "ord_123"})
Async Flow (Server-Generated ID)
import asyncio
async def process_checkout():
flow = await timber.flow_async("checkout")
flow.info("Started checkout")
flow.info("Payment processed")
flow.info("Order confirmed")
asyncio.run(process_checkout())
Flow Properties
flow = timber.flow("user-onboarding")
print(flow.id) # "user-onboarding-a1b2c3d4"
print(flow.name) # "user-onboarding"
Configuration
All Options
from timberlogs import create_timberlogs
timber = create_timberlogs(
# Required
source="my-app", # Your app/service name
environment="production", # development, staging, production
# Authentication
api_key="tb_live_xxx", # Your Timberlogs API key
# Optional
version="1.2.3", # Your app version
user_id="user_123", # Default user ID
session_id="sess_abc", # Default session ID
batch_size=10, # Logs to batch before sending
flush_interval=5.0, # Auto-flush interval (seconds)
min_level="info", # Minimum level to send
on_error=lambda e: print(e), # Error callback
# Retry configuration
max_retries=3, # Retry attempts
initial_delay_ms=1000, # Initial retry delay
max_delay_ms=30000, # Maximum retry delay
)
Environment Variables
import os
from timberlogs import create_timberlogs
timber = create_timberlogs(
source="my-app",
environment=os.getenv("ENVIRONMENT", "development"),
api_key=os.getenv("TIMBER_API_KEY"),
)
Level Filtering
timber = create_timberlogs(
source="my-app",
environment="production",
api_key="tb_live_xxx",
min_level="warn", # Only send warn and error
)
timber.debug("Not sent") # Filtered
timber.info("Not sent") # Filtered
timber.warn("Sent") # Sent
timber.error("Sent") # Sent
Async Usage
import asyncio
from timberlogs import create_timberlogs
async def main():
timber = create_timberlogs(
source="my-app",
environment="production",
api_key="tb_live_xxx",
)
timber.info("Starting async operation")
# Create async flow with server-generated ID
flow = await timber.flow_async("async-job")
flow.info("Step 1")
flow.info("Step 2")
# Async flush
await timber.flush_async()
# Async disconnect
await timber.disconnect_async()
asyncio.run(main())
Async Context Manager
async def main():
async with create_timberlogs(
source="my-app",
environment="production",
api_key="tb_live_xxx",
) as timber:
timber.info("Auto-flushed on exit")
Context Manager
with create_timberlogs(
source="my-app",
environment="production",
api_key="tb_live_xxx",
) as timber:
timber.info("Logs are auto-flushed on exit")
Low-Level Logging
For full control over log entries:
from timberlogs import LogEntry
timber.log(LogEntry(
level="info",
message="Custom log entry",
data={"custom": "data"},
user_id="user_123",
session_id="sess_abc",
request_id="req_xyz",
tags=["important", "billing"],
))
API Reference
TimberlogsClient Methods
| Method | Description |
|---|---|
debug(message, data?, options?) |
Log debug message |
info(message, data?, options?) |
Log info message |
warn(message, data?, options?) |
Log warning message |
error(message, error_or_data?, options?) |
Log error message |
log(entry) |
Log with full control |
flow(name) |
Create flow (local ID) |
flow_async(name) |
Create flow (server ID) |
set_user_id(user_id) |
Set default user ID |
set_session_id(session_id) |
Set default session ID |
flush() |
Send queued logs immediately |
flush_async() |
Async send queued logs |
disconnect() |
Flush and stop client |
disconnect_async() |
Async flush and stop |
should_log(level) |
Check if level passes filter |
Flow Methods
| Method | Description |
|---|---|
debug(message, data?, options?) |
Log debug message |
info(message, data?, options?) |
Log info message |
warn(message, data?, options?) |
Log warning message |
error(message, error_or_data?, options?) |
Log error message |
id |
Property: flow ID |
name |
Property: flow name |
Releasing
- Bump the version in
pyproject.tomlandtimberlogs/__init__.py - Commit and push to
master - Create a GitHub release with tag
vX.Y.Z(e.g.,v1.1.1) - The
publish.ymlworkflow automatically builds and publishes to PyPI via OIDC trusted publishing
Requirements
- Python 3.8+
- httpx >= 0.24.0
License
MIT License - see LICENSE for details.
Links
Project details
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 timberlogs_client-1.2.0.tar.gz.
File metadata
- Download URL: timberlogs_client-1.2.0.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
798c6351bcd295de5131b66248d55c528eb650f8eb53e9545386893cbfceef87
|
|
| MD5 |
76bd59b062be8acf0749ea08fb690118
|
|
| BLAKE2b-256 |
2754a67f37cbf7626e9322d39f2c979178e0408b3f965222bfc4848c80b132d2
|
Provenance
The following attestation bundles were made for timberlogs_client-1.2.0.tar.gz:
Publisher:
publish.yml on enaboapps/timberlogs-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
timberlogs_client-1.2.0.tar.gz -
Subject digest:
798c6351bcd295de5131b66248d55c528eb650f8eb53e9545386893cbfceef87 - Sigstore transparency entry: 974639362
- Sigstore integration time:
-
Permalink:
enaboapps/timberlogs-python-sdk@73caf19e307f7929b4837a73ffa53b166a0069b4 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/enaboapps
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@73caf19e307f7929b4837a73ffa53b166a0069b4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file timberlogs_client-1.2.0-py3-none-any.whl.
File metadata
- Download URL: timberlogs_client-1.2.0-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c74e4822d915df8c9c9b6efb142bbf41ecc937e1cf189eebbc27dc4535531389
|
|
| MD5 |
87fd4d05dad335d41a277401d0e5f884
|
|
| BLAKE2b-256 |
75fd3b79034b0bb571ed19f11d6e6f55df75659e922506d7ed604a269713e8a3
|
Provenance
The following attestation bundles were made for timberlogs_client-1.2.0-py3-none-any.whl:
Publisher:
publish.yml on enaboapps/timberlogs-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
timberlogs_client-1.2.0-py3-none-any.whl -
Subject digest:
c74e4822d915df8c9c9b6efb142bbf41ecc937e1cf189eebbc27dc4535531389 - Sigstore transparency entry: 974639372
- Sigstore integration time:
-
Permalink:
enaboapps/timberlogs-python-sdk@73caf19e307f7929b4837a73ffa53b166a0069b4 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/enaboapps
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@73caf19e307f7929b4837a73ffa53b166a0069b4 -
Trigger Event:
release
-
Statement type: