How to append to a file in Node?

后端 未结 18 1231
庸人自扰
庸人自扰 2020-11-22 09:23

I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.

fs.writeFile(\'log.txt\'         


        
相关标签:
18条回答
  • 2020-11-22 10:12

    For occasional appends, you can use appendFile, which creates a new file handle each time it's called:

    Asynchronously:

    const fs = require('fs');
    
    fs.appendFile('message.txt', 'data to append', function (err) {
      if (err) throw err;
      console.log('Saved!');
    });
    

    Synchronously:

    const fs = require('fs');
    
    fs.appendFileSync('message.txt', 'data to append');
    

    But if you append repeatedly to the same file, it's much better to reuse the file handle.

    0 讨论(0)
  • 2020-11-22 10:12

    My approach is rather special. I basically use the WriteStream solution but without actually 'closing' the fd by using stream.end(). Instead I use cork/uncork. This got the benefit of low RAM usage (if that matters to anyone) and I believe it's more safe to use for logging/recording (my original use case).

    Following is a pretty simple example. Notice I just added a pseudo for loop for showcase -- in production code I am waiting for websocket messages.

    var stream = fs.createWriteStream("log.txt", {flags:'a'});
    for(true) {
      stream.cork();
      stream.write("some content to log");
      process.nextTick(() => stream.uncork());
    }
    

    uncork will flush the data to the file in the next tick.

    In my scenario there are peaks of up to ~200 writes per second in various sizes. During night time however only a handful writes per minute are needed. The code is working super reliable even during peak times.

    0 讨论(0)
  • 2020-11-22 10:13
    fd = fs.openSync(path.join(process.cwd(), 'log.txt'), 'a')
    fs.writeSync(fd, 'contents to append')
    fs.closeSync(fd)
    
    0 讨论(0)
  • 2020-11-22 10:13

    If you want an easy and stress-free way to write logs line by line in a file, then I recommend fs-extra:

    const os = require('os');
    const fs = require('fs-extra');
    
    const file = 'logfile.txt';
    const options = {flag: 'a'};
    
    async function writeToFile(text) {
      await fs.outputFile(file, `${text}${os.EOL}`, options);
    }
    
    writeToFile('First line');
    writeToFile('Second line');
    writeToFile('Third line');
    writeToFile('Fourth line');
    writeToFile('Fifth line');
    

    Tested with Node v8.9.4.

    0 讨论(0)
  • 2020-11-22 10:14

    Using fs.appendFile or fsPromises.appendFile are the fastest and the most robust options when you need to append something to a file.

    In contrast to some of the answers suggested, if the file path is supplied to the appendFile function, It actually closes by itself. Only when you pass in a filehandle that you get by something like fs.open() you have to take care of closing it.

    I tried it with over 50,000 lines in a file.

    Examples :

    (async () => {
      // using appendFile.
      const fsp = require('fs').promises;
      await fsp.appendFile(
        '/path/to/file', '\r\nHello world.'
      );
    
      // using apickfs; handles error and edge cases better.
      const apickFileStorage = require('apickfs');
      await apickFileStorage.writeLines(
        '/path/to/directory/', 'filename', 'Hello world.'
      );
    })();
    

    Ref: https://github.com/nodejs/node/issues/7560
    Example Execution: https://github.com/apickjs/apickFS/blob/master/writeLines.js

    0 讨论(0)
  • 2020-11-22 10:16
    const inovioLogger = (logger = "") => {
        const log_file = fs.createWriteStream(__dirname + `/../../inoviopay-${new Date().toISOString().slice(0, 10)}.log`, { flags: 'a' });
        const log_stdout = process.stdout;
        log_file.write(logger + '\n');
    }
    
    0 讨论(0)
提交回复
热议问题