Internal Logger #
Kuzzle uses its own opinionated wrapper around Pino to log messages.
The internal logger has 6 priority levels:
- trace (not printed by default)
- debug (not printed by default)
- info
- warn
- error
- fatal
Pino can send the logs to different "transports" like stdout, a file, Loki or Elasticsearch. By default, the logs are printed to stdout.
Usage in an Application #
The application logger is available as app.log as soon as the Backend instance is created.
Each method takes either a message, or an object followed by an optional message. When the object is an Error, it is logged under the err key (with its name, message, stack and, for Kuzzle errors, id and code) and its message is used if you give none.
By default the log level is info. You can change it with the server.appLogs.level configuration key, in a .kuzzlerc file or an environment variable (e.g. kuzzle_server__appLogs__level=debug).
The logger is configured when the Backend instance is created: changing server.appLogs with app.config afterwards has no effect. Use app.log.setLevel() to change the level at runtime.
Example: Log messages and change the log level at runtime
import { Backend } from "kuzzle";
const app = new Backend("black-mesa");
app.start().then(() => {
app.log.debug("debug"); // not printed with the default "info" level
app.log.info("info");
app.log.warn({ userId: "gordon" }, "suspicious activity");
try {
throw new Error("resonance cascade");
} catch (error) {
app.log.error(error); // logged under the "err" key
}
app.log.setLevel("trace"); // change the log level at runtime
app.log.trace("trace"); // printed this time
});The silly and verbose methods still work but are deprecated: they log at the trace and debug levels.
Configure the Logger #
The logger is configured in the server.appLogs section of the configuration:
level: the minimum level to log (default:info),transport: where the logs are sent, as a list oftargets. Each target either uses a preset, or is a raw pino transport configuration.
The available presets are:
| Preset | Output |
|---|---|
stdout | The standard output (default): human-readable when NODE_ENV is development (or unset), JSON lines otherwise |
file | A file, set with presetOptions.destination |
loki | A Grafana Loki server, set with presetOptions.host |
kuzzle-elasticsearch | An Elasticsearch index, in the ECS format, set with presetOptions.node |
Example: Print the logs to stdout and send them to Loki
{
"server": {
"appLogs": {
"level": "info",
"transport": {
"targets": [
{ "preset": "stdout" },
{
"preset": "loki",
"presetOptions": { "host": "http://loki:3100" }
}
]
}
}
}
}The options of each preset are described in the Kuzzle Logger documentation.