@atlantjs.dev/logger
v1.0.4
Published
Base logger library with blacklist filter on metadata.
Downloads
2
Readme
Logger
Base logger library with blacklist filter on metadata.
- This library don't include a "transporter" implementation
- This library don't include a "contextStorage" implementation
Install
npm install @atlantjs.dev/logger
yarn add @atlantjs.dev/logger
Using
Initialize the logger with a transporter implementation:
import { Logger, LogRecord, Transporter } from "@atlantjs.dev/logger";
// Create a transporter implementation
Logger.useTransporter({
log(record: LogRecord): void {
console.log(record);
},
});
const logger = Logger.getLogger();
logger.info("Hello", { world: "!" });
Using metadata
Adding metadata
This kind of metadata affects the logger globally:
Logger.addMetadata("version", "1.0.0");
logger.info("Hello world!");
// name: "default", message: "Hello world!", metadata: { version: "1.0.0" }
Adding record metadata
This kind of metadata are specific metadata for a single log:
logger.error("Request failed", { error: error.message });
// name: "default", message: "Request failed", recordMetadata: { error: "..." }
Adding context metadata
This logger supports setting contextual data using some "continuous-local-storage" library.
Be careful: most implementations using this approach have some performance impact.
Example using cls-hooked to add context information with express middlewares:
import express from "express";
import { createNamespace } from "cls-hooked";
import { Logger } from "@atlantjs.dev/logger";
const app = express();
const loggerNamespace = createNamespace("logger");
// the interface should be directly compatible with cls-hooked namespaces
Logger.useContextStorage(loggerNamespace);
app.use((req, res, next) => {
Logger.runInContext(() => {
Logger.addContextMetadata("requestIp", req.ip);
next();
});
});
app.get("/hello", (req, res) => {
const logger = Logger.getLogger("my-logger");
logger.info("Hello world!");
// name: "my-logger", "message: "Hello world!", contextMetadata: { requestIp: "192.168.0.100" }
res.status(200).send("world");
});
app.listen(8080);
Configuring the metadata blacklist/whitelist
This logger support a list of blacklisted and whitelisted keywords to hide sensitive information from log metadata.
Example:
import { Logger } from "@atlantjs.dev/logger";
Logger.setMetadataBlacklist(["pass"]);
Logger.setMetadataWhitelist(["passportId"]);
const logger = Logger.getLogger();
logger.info("User signed in", {
username: "Fran",
passportId: "PST-1493",
password: "secret",
});
// the metadata will be transformed to: {
// "username": "Fran",
// "passportId": "PST-1493",
// "password": "*sensitive*"
// }