fs writefile new line not working

anonprophet

i want log my user command

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

lyjackal

fs.writeFile writes a WHOLE NEW file. What your are looking for is fs.appendFile which will make the file if it doesn't exist and append to it. 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 + '"');
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related