@logtape/koa
v2.0.4
Published
Koa adapter for LogTape logging library
Maintainers
Readme
@logtape/koa: Koa adapter for LogTape
This package provides Koa middleware for HTTP request logging using LogTape as the backend. It serves as an alternative to koa-logger with structured logging support.
Installation
deno add jsr:@logtape/koa # Deno
npm add @logtape/koa # npm
pnpm add @logtape/koa # pnpm
yarn add @logtape/koa # Yarn
bun add @logtape/koa # BunUsage
import Koa from "koa";
import { configure, getConsoleSink } from "@logtape/logtape";
import { koaLogger } from "@logtape/koa";
await configure({
sinks: { console: getConsoleSink() },
loggers: [
{ category: ["koa"], sinks: ["console"], lowestLevel: "info" }
],
});
const app = new Koa();
// Basic usage - should be used near the top of middleware stack
app.use(koaLogger());
app.use((ctx) => {
ctx.body = { hello: "world" };
});
app.listen(3000);Options
app.use(koaLogger({
category: ["myapp", "http"], // Custom category (default: ["koa"])
level: "debug", // Log level (default: "info")
format: "dev", // Predefined format (default: "combined")
skip: (ctx) => ctx.path === "/health", // Skip health check endpoint
logRequest: false, // Log after response (default: false)
}));Predefined formats
"combined"- Apache Combined Log Format with all properties (default)"common"- Apache Common Log Format (without referrer/userAgent)"dev"- Concise output for development"short"- Shorter format with remote address"tiny"- Minimal output
Custom format
app.use(koaLogger({
format: (ctx, responseTime) => ({
method: ctx.method,
path: ctx.path,
status: ctx.status,
duration: responseTime,
}),
}));License
Distributed under the MIT License. See the LICENSE file for details.
