Elysia
@uloki/elysia — drop-in Loki logging plugin for Elysia
Add Loki logging to your Elysia server with a single .use() call.
Installation
pnpm add @uloki/elysiaQuick Start
import { Elysia } from "elysia";
import { uloki } from "@uloki/elysia";
const app = new Elysia()
.use(uloki({
endpoint: "http://localhost:3100",
labels: { app: "my-api" },
}))
.get("/", () => "ok")
.listen(3000);How It Works
The plugin hooks into Elysia's lifecycle:
onRequest— Captures request start timeonAfterResponse— Logs method, path, status, duration with auto-derived log levelonStop— Flushes remaining entries on server shutdown
Configuration
uloki({
endpoint: "http://localhost:3100", // Loki push endpoint
labels: { app: "my-api" }, // Default labels
enabled: true, // Enable/disable
batchSize: 10, // Entries per batch
flushInterval: 5000, // Auto-flush interval (ms)
redact: ["token", "password"], // Redaction patterns
requestLogging: true, // Auto-capture HTTP requests (default: true)
})requestLogging
When true (default), every HTTP request is automatically logged with structured metadata:
method=GET path=/api/users status=200 duration_ms=12Set to false if you only want manual logging.
Manual Logging
Access the logger via store.loki in any route handler:
app.get("/users/:id", ({ store, params }) => {
store.loki.log({
line: `User ${params.id} accessed`,
labels: { action: "view_profile" },
});
return { id: params.id };
});
// Flush manually if needed
await store.loki.flush();Complete Example
import { Elysia } from "elysia";
import { uloki } from "@uloki/elysia";
const app = new Elysia()
.use(uloki({
endpoint: "http://loki:3100",
labels: { app: "my-service", env: "production" },
batchSize: 20,
flushInterval: 10000,
redact: ["token", "secret", /Bearer\s+\S+/g],
}))
.get("/health", () => ({ status: "ok" }))
.get("/users/:id", ({ store, params }) => {
store.loki.log({
line: `Fetching user ${params.id}`,
labels: { action: "get_user" },
});
return { id: params.id };
})
.listen(3000);
console.log(`Server running at ${app.server?.url}`);Requirements
- Elysia >= 1.0
- Node.js >= 22 (or Bun)