[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()
    }))
}

 

posted @ 2023-04-26 15:47  Zhentiw  阅读(11)  评论(0编辑  收藏  举报