Elysia Framework Logging Integration
Add structured logging to Elysia applications running on Bun with lifecycle hooks, scoped context, and trace propagation.
LogTide’s Elysia SDK provides a Bun-optimized plugin for automatic request logging, scoped context, lifecycle hooks, and W3C trace propagation.
Why use LogTide with Elysia?
- Bun-optimized: Takes advantage of Bun’s performance
- Plugin architecture: Use
.as('global')for app-wide coverage - Lifecycle hooks: onRequest, onAfterHandle, onError
- Scoped context: Per-request scope via store decorators
Prerequisites
- Bun 1.x
- Elysia 1.x
- LogTide instance with a DSN
Installation
bun add @logtide/elysia
Quick Start
import { Elysia } from 'elysia';
import { hub } from '@logtide/core';
import { logtide } from '@logtide/elysia';
const app = new Elysia()
.use(logtide({
dsn: process.env.LOGTIDE_DSN,
service: 'elysia-api',
environment: process.env.NODE_ENV,
}))
.get('/users/:id', ({ params }) => {
hub.captureLog('info', 'Fetching user', { userId: params.id });
return { id: params.id };
})
.listen(3000);
Use .as('global') to apply the plugin to all routes, including those registered after.
Plugin Options
app.use(logtide({
dsn: process.env.LOGTIDE_DSN,
service: 'elysia-api',
environment: process.env.NODE_ENV,
release: '1.0.0',
tracesSampleRate: 1.0,
}));
Scoped Context
app
.use(logtide({
dsn: process.env.LOGTIDE_DSN,
service: 'elysia-api',
}))
.get('/orders', () => {
hub.captureLog('info', 'Listing orders');
return { orders: [] };
});
Lifecycle Hooks
| Hook | Behavior |
|---|---|
onRequest | Creates scope, extracts traceparent, starts span |
onAfterHandle | Logs completion with duration |
onError | Captures errors with request context |
Error Handling
app
.use(logtide({
dsn: process.env.LOGTIDE_DSN,
service: 'elysia-api',
}))
.onError(({ code, error }) => {
// Error is already captured by the plugin
return new Response(JSON.stringify({
error: error.message,
}), {
status: code === 'NOT_FOUND' ? 404 : 500,
});
});
Next Steps
- JavaScript SDK - Core SDK reference
- Elysia SDK Reference - Full API documentation
- Docker Integration - Container deployments
Frequently Asked Questions
How do I add LogTide logging to an Elysia application?
Install @logtide/elysia with bun add @logtide/elysia, then call app.use(logtide({ dsn, service, environment })) in your Elysia setup. Use .as('global') on the plugin to ensure all routes, including those registered after the plugin, are covered.
Does the LogTide Elysia plugin automatically log every request?
Yes. The plugin hooks into Elysia's onRequest lifecycle to start a span and extract any incoming traceparent header, and into onAfterHandle to log the completed request with its duration, with no per-route code required.
How does LogTide handle Elysia errors and exceptions?
The plugin registers an onError lifecycle hook that captures errors with full request context automatically. You can still add your own onError handler to return a custom response; the error is already recorded by LogTide before your handler runs.
Does LogTide flush pending logs when the Elysia server stops?
Buffered events are flushed automatically on the batching interval. For graceful shutdown, call hub.flush() (or hub.close()) before the process exits to ensure no logs are lost — the plugin does not register a shutdown hook itself.