Node.js - logging / Use morgan and winston

一世执手 提交于 2019-11-28 15:07:33
lindsaymacvean

This article does an excellent job for what you want to do.

http://tostring.it/2014/06/23/advanced-logging-with-nodejs/

For your specific code you probably need something like this:

var logger = new winston.Logger({
    transports: [
        new winston.transports.File({
            level: 'info',
            filename: './logs/all-logs.log',
            handleExceptions: true,
            json: true,
            maxsize: 5242880, //5MB
            maxFiles: 5,
            colorize: false
        }),
        new winston.transports.Console({
            level: 'debug',
            handleExceptions: true,
            json: false,
            colorize: true
        })
    ],
    exitOnError: false
}),

logger.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};

app.use(require("morgan")("combined", { "stream": logger.stream }));

This will set up Winston to write a log to the console as well as a file. Then you can use the last expression to pass output from the morgan middleware into winston.

In Typescript:

let logger = new (winston.Logger)({
    exitOnError: false,
    level: 'info',
    transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: 'app.log'})
    ]
})

class MyStream {
    write(text: string) {
        logger.info(text)
    }
}
let myStream = new MyStream()
app.use(morgan('tiny', { stream: myStream }));

Update the last line to remove warning

app.use(require("morgan")("combined", { stream: logger.stream }));

Morgan has the bad habit of ending the message with a \n so to make things orderly you may want to remove that before writing it to winston.

This can be done in many different ways like on the format side in winston, or by updating your stream to not write the \n

class MyStream {
    write(text: string) {
        logger.info(text.replace(/\n$/, '');
    }
}
let myStream = new MyStream()
app.use(morgan('tiny', { stream: myStream }));

for Typescript another way to go about it, without needing to create a class is

let logger = new (winston.Logger)({
    exitOnError: false,
    level: 'info',
    transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: 'app.log'})
    ]
})

const myStream = {
    write: (text: string) => {
        logger.info(text)
    }
}

app.use(morgan('combined', { stream: myStream }));

This solution was hived from this Github page https://github.com/winstonjs/winston/issues/1385. However, it is important to note that there is a slight difference between our codes. Instead of:

app.use(morgan('combined', { myStream }));

I use:

app.use(morgan('combined', { stream: myStream }));

This helped me out as am not too big on creating classes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!