Logger
Monitor incoming requests with LykosJS built-in logger middleware, or implement custom request logging for your application.
Use the built-in logger
LykosJS provides loggerMiddleware, a ready-to-use middleware that logs HTTP request information to the console. It records the request method, path, response status, timestamp and execution duration.
1import { Lykos, loggerMiddleware } from 'lykosjs'2 3const app = new Lykos()4 5app.use(loggerMiddleware)6 7app.GET('/', async (ctx, reply) => {8 return reply.json({ message: 'Welcome to LykosJS 🐺' }, 200)9})Register loggerMiddleware usingapp.use() to apply it globally to routes registered afterward.
Understand the log output
Each completed request produces a log entry containing the timestamp, HTTP method, request path, response status and execution duration.
1[LykosJS] 2026-11-26T10:30:00.000Z | GET /users | 200 | 2.45ms2[LykosJS] 2026-11-26T10:30:01.000Z | POST /users | 201 | 5.18msThe logger uses ANSI terminal colors to distinguish the log prefix, request information and timing details.
- Timestamp — The request completion timestamp in ISO 8601 format.
- Method — The HTTP method from
ctx.method. - Path — The request path from
ctx.path. - Status — The HTTP response status code.
- Duration — The elapsed time in milliseconds, formatted to two decimal places.
Register the logger first
Global middleware is captured when a route is registered. Therefore, register the logger before defining routes to ensure that those routes include it in their middleware pipeline.
1// 1. Register global middleware2app.use(loggerMiddleware)3 4// 2. Register routes afterward5app.GET('/users', listUsers)6app.GET('/products', listProducts)7app.POST('/users', createUser)Log failed requests
The built-in logger handles both successful and failed request execution. If next() throws an error, the logger prints a failure message and rethrows the original error. Its finally block still records the request information.
1[LykosJS] Request failed. Error: User not found2[LykosJS] 2026-11-26T10:30:02.000Z | GET /users/123 | 500 | 1.82msWrite a custom logger
If you need a different log format, additional metadata or another logging destination, you can implement your own middleware using the ContextMiddleware type.
1import type { ContextMiddleware } from 'lykosjs'2 3export const requestLogger: ContextMiddleware = async (4 ctx,5 reply,6 next,7) => {8 const started = performance.now()9 10 const response = await next()11 12 const duration = (performance.now() - started).toFixed(2)13 14 console.log(15 `[Request] ${ctx.method} ${ctx.path} | ${response.status} | ${duration}ms`,16 )17 18 return response19}Awaiting next() allows the middleware to measure the execution time of the downstream middleware pipeline and access the resulting response status.
Attach middleware to a group or route
You can also apply custom middleware to a specific route or a group of routes instead of registering it globally.
1// Apply to every route inside the group2app.group(3 '/api/admin',4 (admin) => {5 admin.GET('/reports', getReports)6 admin.GET('/users', getUsers)7 },8 requestLogger,9)10 11// Apply to one specific route12app.GET('/health', healthHandler, requestLogger)Group middleware is applied to the routes registered inside that group. Route-level middleware applies only to the corresponding route.
Common mistakes
1// Incorrect: registering the logger after the route2app.GET('/users', listUsers)3app.use(loggerMiddleware)4 5// Correct: registering the logger before the route6app.use(loggerMiddleware)7app.GET('/users', listUsers)8 9// Incorrect: not returning the downstream response10const brokenLogger: ContextMiddleware = async (ctx, reply, next) => {11 await next()12}13 14// Correct: return the downstream response15const workingLogger: ContextMiddleware = async (ctx, reply, next) => {16 const response = await next()17 return response18}