简体   繁体   English

fs writefile新行无效

[英]fs writefile new line not working

i want log my user command 我想要log我的用户命令

function saveLog (nick, command) {
    var file = 'log/' + nick + '.log';
    var datetime = '[' + getDateTime() + '] ';
    var text = datetime + command + '\r\n';
    fs.writeFile(file, text, function (err) {
        if (err) return console.log(err);
        console.log(text);
    });
}

the function i made is fine, but it didnt save the log in new line, its just replace text / rewrite the file. 我做的功能很好,但它没有在新行中保存日志,它只是替换文本/重写文件。 whats im missing ? 什么我不见了?

thanks 谢谢

fs.writeFile writes a WHOLE NEW file. fs.writeFile写一个WHOLE NEW文件。 What your are looking for is fs.appendFile which will make the file if it doesn't exist and append to it. 您正在寻找的是fs.appendFile ,如果该文件不存在并将其附加到该文件中。 Documentation here . 文档在这里

function saveLog (nick, command) {
    var file = 'log/' + nick + '.log';
    var datetime = '[' + getDateTime() + '] ';
    var text = datetime + command + '\r\n';
    fs.appendFile(file, text, function (err) {
        if (err) return console.log(err);
        console.log('successfully appended "' + text + '"');
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM