我想存储使用 morgan 的网络日志并表达到 json 文件

I want store web log that using morgan and express to json file

我要存储JSON个文件喜欢的形式

[
 {
   "remote-addr" : "127.0.0.1",
   "date"  : " 2018.07.28"
 }
] 

我用这个代码

var format=json(
    ':remote-addr:date'
);

app.use(logger({
    format:format,
    stream: fs.createWriteStream('log.json')
}));  

我使用此代码并获得

{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:41 GMT"}
{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:41 GMT"}
{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:42 GMT"}
{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:48 GMT"}

这是 json 文件,但没有 [ ] 和 ,

如何获取json文件??

从技术上讲,Morgan 不允许您这样做,因为它的全部目的是为每个请求编写 一个标准 access.log(归功于 Douglas Wilson指出这一点)。

是的,您可以像以前一样绕过单个日志行,使其成为有效的 JSON 行。但是,为了使您的 log.json 文件也成为有效的 JSON,我能想到的唯一方法是对文件实施某种 post 处理。

下面是 post 处理过程:首先,您需要读取 line by line 文件。然后,创建您的有效 JSON。最后 - 将它保存在一个单独的文件中(或者无论如何覆盖 log.json)。

这是我的做法。输入:你当前的log.json个文件:

{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:41 GMT"}
{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:41 GMT"}
{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:42 GMT"}
{"remote-addr":"::ffff:127.0.0.1","date":"Sat, 28 Jul 2018 04:38:48 GMT"}

我写的post-处理脚本:

const fs = require('fs');
//  Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable
// readline core module. That's the easiest way to read lines from a file,
// without any external modules. Credits: 
const readline = require('readline');

const lineReader = readline.createInterface({
    input: fs.createReadStream('log.json')
});

const realJSON = [];
lineReader.on('line', function (line) {
    realJSON.push(JSON.parse(line));
});

lineReader.on('close', function () {
    // final-log.json is the post-processed, valid JSON file
    fs.writeFile('final-log.json', JSON.stringify(realJSON), 'utf8', () => {
        console.log('Done!');
    });
});

结果:final-log.json 文件,这是一个 有效的 JSON(我用 jsonlint 验证了它,一切正常)。

[{
    "remote-addr": "::ffff:127.0.0.1",
    "date": "Sat, 28 Jul 2018 04:38:41 GMT"
}, {
    "remote-addr": "::ffff:127.0.0.1",
    "date": "Sat, 28 Jul 2018 04:38:41 GMT"
}, {
    "remote-addr": "::ffff:127.0.0.1",
    "date": "Sat, 28 Jul 2018 04:38:42 GMT"
}, {
    "remote-addr": "::ffff:127.0.0.1",
    "date": "Sat, 28 Jul 2018 04:38:48 GMT"
}]