Small (and fast) utility to log many levels to console in DEV and to stderr in PRODUCTION following "Debug" convention for filtering by namespace
When looking again for a good strategy for debugging and logging I found partial solutions but none fully conforming to my needs :) So I take inspiration from many :
- debug
- json-log
- bole
And forge this (unexpectedly) simple and flexible solution
Instead of complicated implementation, this is more of a convention. The main inspiration is from "debug": every piece of code (module/file/function) name itself trying to not clash with others (follow debug convention). By default nothing is logged but you can enable specific names to be verbose. The main idea of debug, is that you can activate logs even for code that you don't own without complicated configuration but only by setting env variable DEBUG. The idea here is to extend this convention to other levels : INFO, WARN and ERROR.
Then the usage is simple, following standards pratices
const log = require("klogs")("myApp");
log("App starting");
log.debug("env variables", process.env);
const subLog = log.sub("subModule");
subLog.warn("something unexpected", { foo: "bar" });
log.error(new Error("One error occured"));
You can log everything (string, number, boolean, error, request, buffer) at any level (the default level is INFO) If the name is not activated for this level, the log function does nothing at all. If it is activated, it calls the write function with the arguments (plus time and diff)
By default, in a development environnement (process.env = undefinded || "dev" || "development") it write to console and in others environnements it write to stderr in JSON
But it is very easy to use a custom write fonction
// myCustomLog.js
const klogs = require("klogs");
klogs.write = ({ name, time, diff, level, data }) =>
console.error(
`${new Date(time).toISOString()} ${name} ${level} ${data
.map(JSON.stringify)
.join(" ")} +${diff}ms`
);
const log = klogs("myApp");
log({ hello: "world" });
Dont' forget to activate the namespace "myApp" via
INFO=myApp node myCustomLog.js