log-to-file
Advanced tools
Comparing version
76
app.js
@@ -6,33 +6,33 @@ // Import. | ||
* Append zero to length. | ||
* @param {string} value - Value to append zero. | ||
* @param {number} length - Needed length. | ||
* @return {string} | ||
* @param {string} value Value to append zero. | ||
* @param {number} length Needed length. | ||
* @returns {string} | ||
*/ | ||
function appendZeroToLength(value, length) { | ||
var correctValue = value; | ||
if (length > value.toString().length) { | ||
if (value !== 'undefined' && value.toString().length != length) { | ||
// Add zero to correct length. | ||
for (var i = 0; i < length - value.toString().length; i++) { | ||
correctValue = '0' + correctValue; | ||
} | ||
} | ||
} | ||
return correctValue; | ||
var correctValue = value; | ||
if (length > value.toString().length) { | ||
if (value !== 'undefined' && value.toString().length !== length) { | ||
// Add zero to correct length. | ||
for (var i = 0; i < length - value.toString().length; i++) { | ||
correctValue = '0' + correctValue; | ||
} | ||
} | ||
} | ||
return correctValue; | ||
} | ||
/** | ||
* Get date text. | ||
* @return {string} | ||
* Get date as text. | ||
* @returns {string} Date as text. Sample: "2018.12.03, 07:32:13.0162 UTC". | ||
*/ | ||
function getDateText() { | ||
var now = new Date(); | ||
var nowText = appendZeroToLength(now.getUTCFullYear(), 4) + '.' | ||
+ appendZeroToLength(now.getUTCMonth() + 1, 2) + '.' | ||
+ appendZeroToLength(now.getUTCDate(), 2) + ', ' | ||
+ appendZeroToLength(now.getUTCHours(), 2) + ':' | ||
+ appendZeroToLength(now.getUTCMinutes(), 2) + ':' | ||
+ appendZeroToLength(now.getUTCSeconds(), 2) + '.' | ||
+ appendZeroToLength(now.getUTCMilliseconds(), 4) + ' UTC'; | ||
return nowText; | ||
function getDateAsText() { | ||
var now = new Date(); | ||
var nowText = appendZeroToLength(now.getUTCFullYear(), 4) + '.' | ||
+ appendZeroToLength(now.getUTCMonth() + 1, 2) + '.' | ||
+ appendZeroToLength(now.getUTCDate(), 2) + ', ' | ||
+ appendZeroToLength(now.getUTCHours(), 2) + ':' | ||
+ appendZeroToLength(now.getUTCMinutes(), 2) + ':' | ||
+ appendZeroToLength(now.getUTCSeconds(), 2) + '.' | ||
+ appendZeroToLength(now.getUTCMilliseconds(), 4) + ' UTC'; | ||
return nowText; | ||
} | ||
@@ -42,17 +42,17 @@ | ||
* Log to file. | ||
* @param {string} text - Text to log. | ||
* @param {string} [file] - Log file path. | ||
* @param {string} text Text to log. | ||
* @param {string} [file] Log file path. | ||
*/ | ||
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(filename, logText, 'utf8', function(err) { | ||
if (err) { | ||
// If error - show in console. | ||
console.log(getDateText() + ' -> ' + err); | ||
} | ||
}); | ||
// Define file name. | ||
var filename = file !== undefined ? file : 'default.log'; | ||
// Define log text. | ||
var logText = getDateAsText() + ' -> ' + text + '\r\n'; | ||
// Save log to file. | ||
fs.appendFile(filename, logText, 'utf8', function (err) { | ||
if (err) { | ||
// If error - show in console. | ||
console.log(getDateAsText() + ' -> ' + err); | ||
} | ||
}); | ||
} | ||
@@ -59,0 +59,0 @@ |
{ | ||
"name": "log-to-file", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"engines": { | ||
@@ -19,4 +19,4 @@ "node": ">=0.10" | ||
"file", | ||
"error", | ||
"time" | ||
"error", | ||
"time" | ||
], | ||
@@ -29,2 +29,2 @@ "author": "Volodymyr Sichka", | ||
"homepage": "https://gitlab.com/vsichka/log-to-file.npm#README" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # log-to-file | ||
# Installation | ||
## Installation | ||
@@ -20,7 +20,7 @@ ```sh | ||
// Save log to default log file "log.txt". | ||
// Save log to default log file "default.log". | ||
log('Some data'); | ||
// Save log to custom log file "my-log.txt". | ||
log('Another data', 'my-log.txt'); | ||
// Save log to custom log file "my-log.log". | ||
log('Another data', 'my-log.log'); | ||
``` | ||
@@ -32,3 +32,3 @@ | ||
Copyright © 2016 - 2017 Volodymyr Sichka | ||
Copyright © 2016 - 2018 Volodymyr Sichka | ||
@@ -35,0 +35,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
24
test.js
@@ -6,19 +6,19 @@ // Import. | ||
// Log data. | ||
log('Some data to default log file "log.txt".'); | ||
log('Another data to custom log file "my-log.txt".', 'my-log.txt'); | ||
log('Some data to default log file "default.log".'); | ||
log('Another data to custom log file "my-log.log".', 'my-log.log'); | ||
// 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('Default log file:\n' + data); | ||
}); | ||
setTimeout(function () { | ||
fs.readFile('default.log', function (error, data) { | ||
if (error) console.log('Error:\n' + error); | ||
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); | ||
}); | ||
setTimeout(function () { | ||
fs.readFile('my-log.log', function (error, data) { | ||
if (error) console.log('Error:\n' + error); | ||
console.log('Custom log file:\n' + data); | ||
}); | ||
}, 1000); |
4486
3.77%