Custom Transports
Vestig's transport system is designed for extensibility. You can create custom transports to send logs to any destination: databases, message queues, third-party services, or custom storage systems.
Transport Interface
Every transport implements this interface:
Sync vs Async Logging
The log() method can be synchronous or asynchronous:
When to Use Sync
Use synchronous log() when:
- Buffering internally (most common)
- Writing to console
- No I/O operations needed
When to Use Async
Use asynchronous log() when:
- Each log requires immediate I/O
- You need backpressure handling
- Testing or debugging
Warning: Async log() without batching can severely impact performance. The logger awaits each transport's log() method. Prefer BatchTransport for network operations.
Logger Behavior
The logger handles both sync and async transports:
To ensure delivery, call logger.flush():
Quick Start: Simple Transport
The simplest transport just implements name, config, and log:
LogEntry Structure
Every log entry passed to your transport has this structure:
Configuration Options
Transports can accept configuration through TransportConfig:
Configurable Transport Example
Batching with BatchTransport
For network-based transports, use BatchTransport as a base class:
BatchTransport Features
BatchTransport provides:
| Feature | Description |
|---|---|
| Batching | Collects entries until batchSize or flushInterval |
| Auto-flush | Periodic flushing at flushInterval (default: 5s) |
| Retry | Exponential backoff retry on failure |
| Buffer | Circular buffer prevents memory issues |
| Lifecycle | Automatic init/destroy management |
BatchTransport Configuration
Handling Failures
Override error handlers to customize behavior:
Transport Statistics
Get runtime statistics:
Lifecycle Methods
init()
Called once when the logger starts. Use for setup:
flush()
Called to ensure all buffered entries are sent:
destroy()
Called on shutdown for cleanup:
Level Filtering
Transports can filter by level:
Custom Filtering
Use the filter function for complex filtering:
Complete Example: Elasticsearch Transport
Testing Transports
API Reference
Types
BatchTransport Methods
| Method | Description |
|---|---|
log(entry) |
Add entry to buffer |
flush() |
Send all buffered entries |
destroy() |
Stop timer, final flush, cleanup |
getStats() |
Get buffer statistics |
send(entries) |
Abstract - implement to send entries |
onDrop(entries) |
Called when entries are dropped |
onSendError(error, entries) |
Called on send failure |
Best Practices
- Extend BatchTransport for network transports
- Implement destroy() to prevent data loss
- Handle errors gracefully - don't crash the app
- Use level filtering to reduce noise
- Test with mock fetch for HTTP transports
- Add metrics for monitoring transport health