node.js - 快递应用程序中的摩根记录器不会为每个条目创建新行
问题描述
我在让摩根记录器模块将每个日志条目写入日志文件的新行时遇到问题。
我编写了一个快速应用程序来将用户验证到我们订阅的资源中,我需要将日志记录添加到应用程序中。我已将 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
解决方案
摩根实际上写了一个新行:https ://github.com/expressjs/morgan/blob/master/index.js#L130
请注意,这是一个 unix 换行符。如果您在 Windows 上,您应该使用渲染非 Windows 换行符(即不是记事本)的应用程序来读取文件。
推荐阅读
- c++ - 光纤用例
- python - 如何打开存储在多个文件夹Python中的多个netcdf文件
- asp.net-mvc - 如何从视图获取变量到控制器?
- typo3 - Typo3 fe_login 表单未显示在前端
- wpf - 如何更改要使用的数据库的路径?
- postgresql - 如何在 postgres 9.6 中创建 pg_background 扩展?
- perl - 将字符串转换为 CRC-32 时的不同结果
- c# - C# 如何使用 SignedXml 使用 XAdES 正确签署消息?
- javascript - 反应原生 | 将 prop 值传递给 setState
- java - 如何将活动的弱接口类型传递给单例类