Skip to content

Instantly share code, notes, and snippets.

@KrishGarg
Last active October 1, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrishGarg/b2deb27219883fce1e4a899e26fb834f to your computer and use it in GitHub Desktop.
Save KrishGarg/b2deb27219883fce1e4a899e26fb834f to your computer and use it in GitHub Desktop.
logger.js with winston for node projects. Made for personal projects but it is public so if anyone wants to use this config, go ahead.
const { createLogger, format, transports } = require("winston");
const { printf, colorize, timestamp, combine, label } = format;
const { Console, File } = transports;
const myFormat = printf(({ timestamp, message, level, label }) => {
const date = new Date(timestamp).toUTCString();
return `${label ? `[${label}]` : ""}(${date}) [${level}]: ${message}`;
});
const logger = createLogger({
level: "silly",
});
if (process.env.NODE_ENV !== "production") {
logger.add(
new Console({
format: combine(colorize(), timestamp(), myFormat),
})
);
logger.add(
new File({
filename: "combined.log",
format: combine(label({ label: "DEV" }), timestamp(), myFormat),
})
);
} else {
logger.add(
new File({
filename: "combined.log",
format: combine(label({ label: "PROD" }), timestamp(), myFormat),
})
);
}
module.exports = logger;
Winston Logger Config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment