logger
Write readable logs in development and structured logs in production.
What it is
@kitr/logger is a small logger with levels, child loggers that carry fields, and two output formats: readable in development, JSON in production. On the server every request gets a child logger with its request id.
Runs on the server and in the browser. For alerts about errors, use monitoring.
Install
kitr add loggerFirst time? Run npm i -g @kitr/kitr once. Inside a project, the global kitr runs your project's own version.
Adds @kitr/logger, its provider and config/logger.ts. server adds it for you. Every change.
Quick start
In a controller, the logger is ctx.logger, a child of the app's logger that adds the request id to every line:
// server/controllers/orders_controller.ts
import type { HttpContext } from '@kitr/api'
export default class OrdersController {
async ship({ logger, params }: HttpContext<{ id: string }>) {
logger.info('order shipped', { orderId: params.id })
return { orderId: params.id, status: 'shipped' }
}
}
Call POST /api/orders/1042/ship in development and the server prints:
INFO 14:03:22.418 [app] order shipped {"requestId":"9b2f6c1e-4f3a-4c1d-8e7a-2b5d0c9e1f44","orderId":"1042"}
In production, where NODE_ENV=production, the same call writes one JSON object per line, ready for your log service:
{"level":"info","time":"2026-11-07T14:03:22.418Z","name":"app","requestId":"9b2f6c1e-4f3a-4c1d-8e7a-2b5d0c9e1f44","orderId":"1042","msg":"order shipped"}
Keep the message fixed and put the variable parts in fields, so a log service can count every order shipped line. Next in the guide: Write your first log line.
Built on
kitr adds the request-scoped child logger, the pretty format, timing, run-time level changes and the devtools mirror.
Problems it solves
- Unreadable logs in development, unsearchable logs in production. Choose the output format.
- Repeating the same fields on every line. Add fields to every line.
- Lines you cannot tie to a request. Log during a request.
- Errors logged as
[object Object]. Log an error. - Timing by hand. Time an operation.
- Restarting to see debug output. Change the level at run time.
Further reading
- Guide.
- API reference.
- Server.
- Monitoring and Devtools.
- Configuration.