Application
Logging
A built-in logger in the NestJS format, with colours, levels and a JSON mode.
Logging is how an application tells you what it is doing. Kest has a logger built in (Logger, in core), so every package logs the same way and you can redirect all of it in one place.
@Injectable
class UserService {
private val log = Logger(UserService::class) // named after the class; Java: Logger.of(UserService.class)
fun create(u: User) {
log.log("created ${u.id}") // normal information
log.debug("details: $u") // only while debugging
log.warn("slow response from the mail server") // something odd, not broken
log.error("failed to save", exception) // a failure; the stack trace is printed
log.fatal("giving up") // the application cannot continue
}
}The output has the NestJS layout, one colour per level:
[Kest] 12345 - 11/12/2026, 10:15:30 AM LOG [KestFactory] Starting Kest application...
[Kest] 12345 - 11/12/2026, 10:15:30 AM LOG [InstanceLoader] UsersModule dependencies initialized +1ms
[Kest] 12345 - 11/12/2026, 10:15:30 AM LOG [RoutesResolver] UserController {/users}:
[Kest] 12345 - 11/12/2026, 10:15:30 AM LOG [RouterExplorer] Mapped {/users/{id}, GET} route +2ms
[Kest] 12345 - 11/12/2026, 10:15:31 AM WARN [UserService] Cache is cold +1203ms
[Kest] 12345 - 11/12/2026, 10:15:32 AM ERROR [OrderService] Payment failed
java.lang.IllegalStateException: ...The framework logs its own start-up in the same structure (KestFactory, InstanceLoader, RoutesResolver, RouterExplorer, KestApplication), and its errors under ExceptionsHandler and the component name.
| Level | Use it for | Colour | Stream |
|---|---|---|---|
VERBOSE | very detailed tracing | cyan | stdout |
DEBUG | information useful when diagnosing | magenta | stdout |
LOG | normal events worth recording (the default level) | green | stdout |
WARN | unexpected but handled | yellow | stderr |
ERROR | a failure, with its stack trace | red | stderr |
FATAL | the application cannot go on | bold red | stderr |
The [Kest] prefix and the pid are green, the context yellow, and +Nms (yellow) is the time since the previous message from the same context. Colours switch on when the output is a terminal and NO_COLOR is not set; force them with FORCE_COLOR=1 or -Dkest.log.colors=true (IDE run windows are not terminals), and off with -Dkest.log.colors=false.
Settings
Logging.level = LogLevel.DEBUG // the lowest level shown (LOG by default)
Logging.configure { prefix = "Shop"; colors = false; timestamp = true; timeDiff = false }
Logging.configure { json = true } // one JSON object per line, for log collectorsProduction: JSON logs
Humans read coloured text; machines read JSON. In production, use json = true: each line is one object that a log collector (Loki, Elasticsearch, CloudWatch) can index and search:
{"level":"error","pid":123,"timestamp":1731400000000,"message":"Payment failed","context":"OrderService","error":{"type":"java.lang.IllegalStateException","message":"...","stack":"..."}}Put the trace id in your messages so a log line can be found from its trace: log.log("charging (trace ${tracing.currentTraceId()})"), or use the LGTM stack sinks, which add it for you.
Send logs somewhere else
All logging goes through one sink. Replace it to write to a file, a service, or several places at once:
Logger.overrideLogger(LogSink { level, context, message, error -> myAppender.write(level, context, message, error) })
Logger.overrideLogger(LokiLogSink("http://loki:3100", labels = mapOf("service" to "shop"))) // package lgtmWhat to log
- Do log decisions and boundaries: a payment started and finished, an external call failed, a retry happened.
- Do not log secrets or personal data (passwords, tokens, card numbers). Logs are copied, kept and read by many people.
- One line per event, with the ids needed to find the case (
order 42), not entire objects. - Do not log in tight loops; use a metric for counts.