首页 > 解决方案 > 快递应用程序中的摩根记录器不会为每个条目创建新行

问题描述

我在让摩根记录器模块将每个日志条目写入日志文件的新行时遇到问题。

我编写了一个快速应用程序来将用户验证到我们订阅的资源中,我需要将日志记录添加到应用程序中。我已将 morgan 设置为写入文件,但是,日志文件中的每个条目都写入同一行,而不是写入新行。

这是我的代码中的内容

//requiring express and morgan
var express = require('express');
var morgan = require('morgan');

//defining the file to write log entries to
var accessLogStream = fs.createWriteStream(path.join('logs', 'access.log'), {flags: 'a'});

//defining what to log with morgan
app.use(morgan(':date[iso], :remote-addr, :status, :response-time ms', {stream: accessLogStream}));

我错过了什么?我尝试将 /n 添加到摩根格式的末尾,例如:

app.use(morgan(':date[iso], :remote-addr, :status, :response-time ms /n', {stream: accessLogStream}));

但这导致了错误。错误说:

undefined:8
(tokens["response-time](req, res) || "-") + " ms

SyntaxError: Invalid or unexpeceted token

标签: node.jsexpressloggingmorgan

解决方案


摩根实际上写了一个新行:https ://github.com/expressjs/morgan/blob/master/index.js#L130

请注意,这是一个 unix 换行符。如果您在 Windows 上,您应该使用渲染非 Windows 换行符(即不是记事本)的应用程序来读取文件。


推荐阅读