NodeJS写日志文件(Json)

时间:2014-08-14 10:17:39

标签: json node.js logging

我想将日志写入Json文件

newEntry =  "User: "  + lastUsername + " Time: "+now+ " Door: "+IOSDoor;
lastUserOpenClose += newEntry;

jsonString = JSON.stringify(lastUserOpenClose);

fs.writeFile("lastUserOpenClose.json", lastUserOpenClose, function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("Server-Log: The file was saved!");
    }
});

但我正在覆盖日志。 如何将somthing写入json文件而不覆盖旧日志?

1 个答案:

答案 0 :(得分:12)

您应该使用fs.appendFile

示例:

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
  console.log('The "data to append" was appended to file!');
});