In software development, logs serve as a record of events within an application, providing crucial insights for debugging. When logging, it is
essential to ensure that the logs are:
- easily accessible
- uniformly formatted for readability
- properly recorded
- securely logged when dealing with sensitive data
Those requirements are not met if a program directly writes to the standard outputs (e.g., console). That is why defining and using a dedicated
logger is highly recommended.
Code examples
The following noncompliant code:
function doSomething() {
// ...
console.log("My Message");
// ...
}
In Node.js
could be replaced by the winston
logging library:
const winston = require("winston");
const logger = winston.createLogger({
level: "debug",
format: winston.format.json(),
transports: [new winston.transports.Console()],
});
function doSomething() {
// ...
logger.info("My Message");
// ...
}