json - NodeJS 写入日志文件(Json)

标签 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!");
    }
});

但我正在覆盖日志。 如何将内容写入 json 文件并且不覆盖旧日志?

最佳答案

你应该使用 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!');
});

关于json - NodeJS 写入日志文件(Json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25305622/

相关文章:

node.js - 如何计算 Mongoose 中两个地理空间之间的距离?

node.js - Nightmare 不会启动-我的编码中出现语法错误(输入意外结束)

c# - 城堡记录设施

java - 如何避免使用 log4j 丢失日志

php - 如何将字符串和数据从 iOS 发送到 Php 以在 MySQL 数据库中创建表

java - JSON 响应中途中断

android - 无尽的回收者 View 重复 json 数据

javascript - Node.js 作为 JSON 转发器

node.js - NodeJS 请求处理程序

c++ - Boost Log Formatter 使用与关键字相同的字符串