logger 0.0.1
Install from the command line:
Learn more about npm packages
$ npm install @baselime/logger@0.0.1
Install via package.json:
"@baselime/logger": "0.0.1"
About this version
A TypeScript logger with all the resources to manage request and trace ids.
LOG_LEVEL
sets the logging level to use (default info
). This can be one of debug
, info
, warn
or error
. The setting will log anything at the chosen level and above.
Use the bindExpressMiddleware
function to get a single trace id and request id per request. This will look for the x-trace-id
an x-request-id
headers on the incoming request and use them, or else it will create ones.
import express from "express";
import logger from "@baselime/logger"
const app = express();
app.use(logger.bindExpressMiddleware);
app.get('/', homePage);
function homePage(req, res) {
const message = "Typing something";
logger.info("Got the message for the user", { message });
res.send(message);
}
app.listen(3000);
import logger from "@baselime/logger";
export function doSomething(input: any) {
const requestId = "request-id";
const traceId = "trace-id";
return logger.bindFunction(doSomethingFunction, requestId, traceId)(input);
}
function doSomethingFunction(input: any) {
try {
logger.info("I got the input", { input });
} catch(err) {
logger.error("Houston we have a problem", { error: err, input });
}
}
By default this logger does not log debug logs. These can be activated using the environment variable at application. For a more granular control of which debug logs to activate, the following pattern can be used.
import logger from "@baselime/logger";
logger.debug("This debug log will not be logged");
logger.enableDebug();
logger.debug("This debug log will be logged");
logger.disableDebug();
logger.debug("This debug log will not be logged);