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
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
Share Improve this question asked May 17, 2015 at 4:54 anonprophetanonprophet 971 gold badge3 silver badges11 bronze badges 1 |1 Answer
Reset to default 19fs.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 + '"');
});
}
fs.appendFile
– laggingreflex Commented May 17, 2015 at 4:58