log-to-file
Advanced tools
Comparing version
@@ -32,7 +32,9 @@ // Import. | ||
// Log to file. | ||
function logToFile(text) { | ||
function logToFile(text, file) { | ||
// Define file name. | ||
var filename = file !== undefined ? file : 'log.txt'; | ||
// Define log text. | ||
var logText = getDateText() + ' -> ' + text + '\r\n'; | ||
// Save log to file. | ||
fs.appendFile('log.txt', logText, 'utf8', function(err) { | ||
fs.appendFile(filename, logText, 'utf8', function(err) { | ||
if (err) { | ||
@@ -39,0 +41,0 @@ // If error - show in console. |
{ | ||
"name": "log-to-file", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Log to file", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,4 +21,7 @@ log-to-file | ||
// Save log to file "log.txt". | ||
// Save log to default log file "log.txt". | ||
log('Some data'); | ||
// Save log to custom log file "my-log.txt". | ||
log('Another data', 'my-log.txt'); | ||
``` | ||
@@ -25,0 +28,0 @@ |
16
test.js
@@ -6,11 +6,19 @@ // Import. | ||
// Log data. | ||
log('Some data.'); | ||
log('Another data.'); | ||
log('Some data to default log file "log.txt".'); | ||
log('Another data to custom log file "my-log.txt".', 'my-log.txt'); | ||
// Wait 1 second and check log file. | ||
// Wait 0,5 second and check default log file. | ||
setTimeout(function() { | ||
fs.readFile('log.txt', function(err, data) { | ||
if (err) console.log('Error:\n' + err); | ||
console.log('Log file:\n' + data); | ||
console.log('Default log file:\n' + data); | ||
}); | ||
}, 500); | ||
// Wait 1 second and check custom log file. | ||
setTimeout(function() { | ||
fs.readFile('my-log.txt', function(err, data) { | ||
if (err) console.log('Error:\n' + err); | ||
console.log('Custom log file:\n' + data); | ||
}); | ||
}, 1000); |
Sorry, the diff of this file is not supported yet
4088
14.8%61
17.31%38
8.57%