[Node.js] Logging with winston package
logger.ts
// .env
LOGGER_LEVEL=debug
// logger.ts
import * as winston from "winston";
export const logger = winston.createLogger({
level: process.env.LOGGER_LEVEL,
format: winston.format.json({space: 4}),
transports: [
// log all logs into logs/all.log file
new winston.transports.File({
filename: "logs/all.log"
}),
// only log error into logs/error.log file
new winston.transports.File({
filename: "logs/error.log",
level: "error"
})
]
})
if (process.env.NODE_ENV != "production") {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}))
}