@logtape/elysia
v2.0.5
Published
Elysia adapter for LogTape logging library
Maintainers
Readme
@logtape/elysia
This package provides an Elysia plugin for HTTP request logging using LogTape as the backend.
Installation
deno add jsr:@logtape/elysia # for Deno
npm add @logtape/elysia # for npm
pnpm add @logtape/elysia # for pnpm
yarn add @logtape/elysia # for Yarn
bun add @logtape/elysia # for BunUsage
import { Elysia } from "elysia";
import { configure, getConsoleSink } from "@logtape/logtape";
import { elysiaLogger } from "@logtape/elysia";
await configure({
sinks: { console: getConsoleSink() },
loggers: [
{ category: ["elysia"], sinks: ["console"], lowestLevel: "info" }
],
});
const app = new Elysia()
.use(elysiaLogger())
.get("/", () => ({ hello: "world" }))
.listen(3000);
console.log(`Server running at ${app.server?.url}`);Options
The elysiaLogger() function accepts an optional options object:
app.use(elysiaLogger({
category: ["myapp", "http"], // Custom category (default: ["elysia"])
level: "debug", // Log level (default: "info")
format: "dev", // Predefined format (default: "combined")
skip: (ctx) => ctx.path === "/health", // Skip logging for specific paths
logRequest: true, // Log at request start (default: false)
scope: "global", // Plugin scope (default: "global")
}));Plugin scope
Elysia supports plugin scoping to control how lifecycle hooks propagate:
"global": Hooks apply to all routes in the application (default)"scoped": Hooks apply to the parent instance where the plugin is used"local": Hooks only apply within the plugin itself
Predefined formats
The plugin supports Morgan-compatible predefined formats:
"combined": Apache Combined Log Format with all properties (default)"common": Apache Common Log Format (without referrer/userAgent)"dev": Concise output for development (e.g.,GET /path 200 1.234 ms - 123)"short": Shorter format with remote address"tiny": Minimal output
Custom format function
You can also provide a custom format function:
app.use(elysiaLogger({
format: (ctx, responseTime) => ({
method: ctx.request.method,
path: ctx.path,
status: ctx.set.status,
duration: responseTime,
}),
}));Error logging
The plugin automatically logs errors at the error level using Elysia's
onError hook. Error logs include the error message and error code
in addition to standard request properties.
Structured logging output
When using the "combined" format (default), the plugin logs structured
data that includes:
method: HTTP request methodurl: Request URLpath: Request pathstatus: HTTP response status coderesponseTime: Response time in millisecondscontentLength: Response content-length header valueremoteAddr: Remote client address (from X-Forwarded-For header)userAgent: User-Agent header valuereferrer: Referrer header value
See also
For more information, see the LogTape documentation.
