【问题标题】:NodeJS: How to read and modify buffer data, before parsing it from file?NodeJS:如何在从文件解析之前读取和修改缓冲区数据?
【发布时间】:2021-11-16 19:49:39
【问题描述】:

在 NodeJS 中,我有一个日志文件,其中我的日志格式为:

{"time":"2021-09-23T11:36:18.076Z","type":"info","message":"some message","data":{"id":123}},
{"time":"2021-09-23T11:36:18.076Z","type":"info","message":"some message","data":{"id":123}},
{"time":"2021-09-23T11:36:18.076Z","type":"info","message":"some message","data":{"id":123}},

这些基本上是对象,用逗号分隔。我需要做的是读取此文件的内容并将日志转换为对象数组(我可以稍后操作)。

我正在尝试类似的东西:

    let fileLogs = "./data/myfile.log";
    fs.readFile(fileLogs, (err, fileLogsContent) => {
      if (err) {
        console.log("cannot read log file");
        return;
      }

      //I know I need to manipulate the fileLogsContent here, before doing JSON.parse

      let logsContent = { ...JSON.parse(fileLogsContent) };
      //do something here with the array of objects 'logsContent'
    });

由于日志文件中的内容不是可以解析的格式,所以上面的JSON.parse失败了。我的想法是将日志文件采用以下格式:

[
{"time":"2021-09-23T11:36:18.076Z","type":"info","message":"some message","data":{"id":123}},
{"time":"2021-09-23T11:36:18.076Z","type":"info","message":"some message","data":{"id":123}},
{"time":"2021-09-23T11:36:18.076Z","type":"info","message":"some message","data":{"id":123}}
]

这意味着我需要在运行中添加[ 作为第一个字符,并将最后一个, 替换为]。我不知道我该怎么做,因为 fileLogsContent 实际上是一个缓冲区。那么如何阅读内容并进行我提到的操作,以便以后能够对其进行解析并将其转换为array of objects 格式?

【问题讨论】:

    标签: node.js buffer readfile streamreader node-streams


    【解决方案1】:

    您可以简单地将每一行包装在一个字符串中,然后在删除尾随逗号后对其调用JSON.parse。这是一个例子(注意它仍然需要对c进行错误处理):

    const fs = require('fs');
    const readline = require('readline');
    const readInterface = readline.createInterface({
        input: fs.createReadStream('./input.txt'),
        output: undefined,
        console: false
    });
    
    (async () => {
        const resultArray = await new Promise((resolve, reject) => {
            const chunks = [];
            readInterface.on('line', (line) => {
                line = line.substr(0, line.lastIndexOf(','))
                chunks.push(JSON.parse(`${line}`));
            })
    
            readInterface.on('close', () => {
                resolve(chunks);
            })
        });
        console.log(resultArray); 
    })();
    

    【讨论】:

    • @delux:有什么反馈吗?
    • 它不起作用,但你确实给了我正确的方向。我发布了我的工作解决方案。
    【解决方案2】:

    这是我根据@eol 的回答提出的可行解决方案。

    const { once } = require('events');
    const { createReadStream } = require('fs');
    const { createInterface } = require('readline');
    
    (async function processLineByLine() {
      try {
        const rl = createInterface({
          input: createReadStream('./data/myfile.log'),
          crlfDelay: Infinity
        });
        const chunks = [];
    
        rl.on('line', (line) => {
          // Process the line.
            chunks.push(JSON.parse(`${line.substr(0, line.lastIndexOf(','))}`));
        });
    
        await once(rl, 'close');
    
        console.log('File processed. Content = ', chunks);
      } catch (err) {
        console.log("cannot read log file, err = ", err);
      }
    })();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多