Skip to content
LykosJSLYKOSJS
CreatorDeveloperBenchmarkshomeGet started
v1.0.0
OverviewCORSLoggerAuthenticationMiddleware Pipeline
PREVIOUSCORSNEXT Authentication
© 2026 LykosJS · MIT LicenseBun · TypeScript

© 2026 LykosJS. Released under the MIT License.
Built by Zen Allaham.

DocsGitHubX
DOCS/V1.0.0/MIDDLEWARES
MIDDLEWARES

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.

ts
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.

ts
1[LykosJS] 2026-11-26T10:30:00.000Z | GET /users | 200 | 2.45ms2[LykosJS] 2026-11-26T10:30:01.000Z | POST /users | 201 | 5.18ms

The 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 fromctx.method.
  • Path — The request path fromctx.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.

ts
1// 1. Register global middleware2app.use(loggerMiddleware)3 4// 2. Register routes afterward5app.GET('/users', listUsers)6app.GET('/products', listProducts)7app.POST('/users', createUser)
Middleware registration order matters
Routes registered before app.use(loggerMiddleware)do not automatically receive the logger. Register global middleware before registering the routes that should use it.

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.

ts
1[LykosJS] Request failed. Error: User not found2[LykosJS] 2026-11-26T10:30:02.000Z | GET /users/123 | 500 | 1.82ms
Failed request status
The logger initializes the status code to 500and updates it only when next() resolves with a response. If the middleware pipeline throws an error, the logged status remains 500, even if another error handler later produces a different HTTP status.

Write 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.

ts
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.

Return the response
A custom middleware should return the response produced bynext() so the downstream response continues through the middleware pipeline.

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.

ts
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

ts
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}

Logging security

Do not log sensitive information
Avoid logging passwords, authorization headers, cookies, access tokens, secrets or complete request bodies. Prefer request metadata and carefully selected diagnostic information.