@larvit/log
Structured logging with a simple interface and support for OTLP.
Installation
npm i @larvit/log or yarn add @larvit/log
Usage
import { Log } from "@larvit/log";
const log = new Log("silly");
log.error("Apocalypse! :O");
log.warn("The chaos is near");
log.info("All is well, but this message is important");
log.verbose("Extra info, likely good in a production environment");
log.debug("A lot of detailed logs to debug your application");
log.silly("Open the flood gates!");
To clone your log instance: const log2 = log.clone();.
Group your logs
To get tracing, timings, spans etc you can group your logs like this example:
import { Log } from "@larvit/log";
const appLog = new Log({
context: {
"service.name": "foobar"
}
});
function myRequsetHandler(req, res) {
const reqLog = new Log({
context: { requestId: crypto.randomUUID() },
parentLog: appLog,
spanName: "request",
});
reqLog.info("Incoming request", { url: req.url });
reqLog.end();
}
Configuration
Log level only
const log = new Log("info"); Will only output error, warn and info logs. This is the default. All possible options: "error", "warn", "info", "verbose", "debug", "silly" and "none".
All options
const log = new Log({
context: {
key: "string",
anotherKey: "string",
},
format: "text",
logLevel: "info",
entryFormatter: ({ logLevel, metadata, msg }) => {
return `${logLevel}: ${msg} ${JSON.stringify(metadata)}`;
},
otlpAdditionalHeaders: null,
otlpHttpBaseURI: null,
parentLog: new Log(),
printTraceInfo: false,
spanName: "my-span",
stdout: console.log,
stderr: console.error,
});
Metadata
log.info("foo", { hey: "luring" }); --> 2022-09-24T23:40:39Z [info] foo {"hey":"luring"}