如何从摩根记录器中删除日志文件中生成的空行?

时间:2016-11-15 04:35:16

标签: node.js logging winston morgan

我在输出中得到空行,如下所示 我在这里使用morgan和winston结合登录文件 如何避免那些空白行?

//logger 
const logger = new winston.Logger({
    transports: [
        new winston.transports.File({
            filename: './logs/dca_ui.log',
            handleExceptions: true,
            json: false,
            maxsize: MAX_SIZE_LOG,
            maxFiles: MAX_LOG_FILES,
            colorize: false,
        }),
     ],
     exitOnError: false,
});

//writestream
const stream = {
    write: (message, encoding) => {
        logger.info(message);
    },
};

app.use((morgan(':date :status :method :url :response-time', { stream: stream })));

输出:

  

5 2016-11-15T04:21:18.981Z - info:Tue,2016年11月15日04:21:18 GMT 200 GET /index.js 4.539   
6   
7 2016-11-15T04:21:19.786Z - info:星期二,2016年11月15日04:21:19 GMT 200 GET / urest / v1 / template?start = 0& count = 20& sort = templateName: asc 106.379   
8   
9 2016-11-15T04:21:22.835Z - info:星期二,2016年11月15日04:21:22 GMT 200 GET / urest / v1 / template / 7b4ca205-7b75-459c-81f1-a61fc8b6be69?view =浓缩122.692

1 个答案:

答案 0 :(得分:5)

morgan在流的末尾添加\ n - 新行。 如果你记录下来,你可以看到它: logger.info(JSON.stringify(message));

所以要删除它,你需要这样的东西:

//writestream
const stream = {
    write: (message, encoding) => {
        logger.info(message.substring(0,message.lastIndexOf('\n')));
    },
};

这是关于它的讨论: https://github.com/expressjs/morgan/issues/70