Recipes
Custom Format and Tokens
A format string picks the fields; a custom token adds one the built-ins do not have. Here the user that an auth handler stores on event.store, which the token reads after the response, and a request header.
import { App, defineCoreHandler } from 'routup';
import { logger } from '@routup/logger';
const UserKey = Symbol.for('app:user');
export const app = new App();
app.use(logger(':method :url :status :response-time[0] ms user=:user id=:req[x-request-id]', {
tokens: {
// a token returns a string, or undefined for '-'
user: (event) => event.store[UserKey] as string | undefined,
},
}));
app.use(defineCoreHandler((event) => {
const token = event.headers.get('authorization');
if (token === 'Bearer ada-token') {
event.store[UserKey] = 'ada';
}
return event.next();
}));
app.get('/', defineCoreHandler(() => 'Hello, World!'));curl -H 'authorization: Bearer ada-token' -H 'x-request-id: r-1' http://localhost:3000/
# server output:
# GET / 200 0 ms user=ada id=r-1Skip Requests
skip gets the event and the response, so it can drop a path, a status range or both. This logger writes only failed requests and never the health check.
import { App, defineCoreHandler } from 'routup';
import { logger } from '@routup/logger';
export const app = new App();
app.use(logger({
// no line for health checks, and none for redirects and successes
skip: (event, response) => event.path === '/health' ||
(!!response && response.status < 400),
}));
app.get('/health', defineCoreHandler(() => 'ok'));
app.get('/', defineCoreHandler(() => 'Hello, World!'));
app.get('/admin', defineCoreHandler((event) => {
event.response.status = 403;
return 'Forbidden';
}));curl http://localhost:3000/ # no line
curl http://localhost:3000/admin
# server output:
# GET /admin 403 - - 0.081 msJSON Lines
A Formatter function returns the whole line, so it can write JSON for a log collector. The tokens are available as functions.
import { App, defineCoreHandler } from 'routup';
import { logger } from '@routup/logger';
export const app = new App();
app.use(logger((tokens, event, response) => JSON.stringify({
method: event.method,
url: tokens.url?.(event, response),
status: response?.status,
ms: Number(tokens['response-time']?.(event, response, '1')),
})));
app.get('/', defineCoreHandler(() => 'Hello, World!'));curl http://localhost:3000/
# server output:
# {"method":"GET","url":"/","status":200,"ms":0.2}Write to a File
write receives each line without a newline; here it appends to a file stream. The same shape sends lines to any stream or structured logger.
import { createWriteStream } from 'node:fs';
import { App, defineCoreHandler } from 'routup';
import { logger } from '@routup/logger';
// appends one line per request
export const accessLog = createWriteStream(process.env.ACCESS_LOG ?? 'access.log', { flags: 'a' });
export const app = new App();
app.use(logger('combined', {
write: (line) => {
accessLog.write(`${line}\n`);
},
}));
app.get('/', defineCoreHandler(() => 'Hello, World!'));curl -u ada:secret -e https://example.com/ 'http://localhost:3000/?page=2'
cat access.log
# ::1 - ada [17/Nov/2026:10:21:03 +0000] "GET /?page=2 HTTP/1.1" 200 - "https://example.com/" "curl/8.7.1"The first field is the client address as the server reports it (::1 for curl on the same machine); it is - when there is none, such as for app.fetch() in a test.
Extend the Dev Format
devFormatter is a Formatter like any other, so a function can call it and add fields.
import { App, defineCoreHandler } from 'routup';
import { devFormatter, logger } from '@routup/logger';
export const app = new App();
app.use(logger((tokens, event, response) => {
const line = devFormatter(tokens, event, response);
return line && `${line} pid=${tokens.pid?.(event, response)}`;
}));
app.get('/', defineCoreHandler(() => 'Hello, World!'));curl http://localhost:3000/
# server output (200 in green):
# GET / 200 0.412 ms - - pid=4242