As of September 2024 this package is deprecated and will receive no further updates.
Any internal changes should be made directly in lu-greenfield
.
File name and line number of the log caller is also added when logging in debug level.
A configuration object must be present in config/<environment>.json
.
{
"loging": {
"log": "stdout", // Or file, required
"logLevel": "debug", // Minimum level to log
"logJson": true // Log JSON objects or string messages, defaults to true
}
}
When log mode file
is enabled, the log will be written to a file at <app root>/logs/<NODE_ENV>.log
directory, where app root
is the folder containing the package.json
file.
Log mode stdout
will log to stdout.
The JSON below is an example of a log entry when logJson
is set to true (or omitted) and logLevel
is set to debug.
{
"metaData": {
"correlationId": "some-epic-id"
},
"level": "debug",
"message": "This is the log message",
"timestamp": "2018-02-21T12:22:19.150Z"
}
This library provides a mechanism for automatically logging debug metadata (e.g. correlation IDs).
This is implemented with inspiration from this article,
by using an AsyncLocalStorage
variable,
which is local to each async
context.
It then provides a middleware factory for Express which sets this from the request and then logs it out automatically.
The calling library is free to define how to get the metadata from the request.
As an example
const express = require("express");
const { debugMeta } = require("lu-logger");
const app = express();
app.use(debugMeta.initMiddleware((req) => req.debugMeta));