Last active
October 1, 2021 15:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Winston Logger Config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment