log-to-file
Advanced tools
Comparing version
31
app.js
// Import. | ||
var fs = require('fs'); | ||
const fs = require('fs'); | ||
@@ -8,15 +8,6 @@ /** | ||
* @param {number} length Needed length. | ||
* @returns {string} | ||
* @returns {string} String with appended zeros id need it. | ||
*/ | ||
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; | ||
return `${value}`.padStart(length, 0); | ||
} | ||
@@ -29,4 +20,4 @@ | ||
function getDateAsText() { | ||
var now = new Date(); | ||
var nowText = appendZeroToLength(now.getUTCFullYear(), 4) + '.' | ||
const now = new Date(); | ||
const nowText = appendZeroToLength(now.getUTCFullYear(), 4) + '.' | ||
+ appendZeroToLength(now.getUTCMonth() + 1, 2) + '.' | ||
@@ -48,10 +39,12 @@ + appendZeroToLength(now.getUTCDate(), 2) + ', ' | ||
// Define file name. | ||
var filename = file !== undefined ? file : 'default.log'; | ||
const filename = file !== undefined ? file : 'default.log'; | ||
// Define log text. | ||
var logText = getDateAsText() + ' -> ' + text + '\r\n'; | ||
const logText = getDateAsText() + ' -> ' + text + '\r\n'; | ||
// Save log to file. | ||
fs.appendFile(filename, logText, 'utf8', function (err) { | ||
if (err) { | ||
fs.appendFile(filename, logText, 'utf8', function (error) { | ||
if (error) { | ||
// If error - show in console. | ||
console.log(getDateAsText() + ' -> ' + err); | ||
console.log(getDateAsText() + ' -> ' + error); | ||
} | ||
@@ -58,0 +51,0 @@ }); |
{ | ||
"name": "log-to-file", | ||
"version": "1.3.0", | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"version": "2.0.0", | ||
"description": "Log text to file (with timestamp)", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "./node_modules/.bin/eslint ./ && node test.js" | ||
}, | ||
@@ -27,3 +24,6 @@ "repository": { | ||
}, | ||
"homepage": "https://gitlab.com/vsichka/log-to-file.npm#README" | ||
} | ||
"homepage": "https://gitlab.com/vsichka/log-to-file.npm#README", | ||
"devDependencies": { | ||
"eslint": "^5.16.0" | ||
} | ||
} |
@@ -17,3 +17,3 @@ # log-to-file | ||
// Import. | ||
var log = require('log-to-file'); | ||
const log = require('log-to-file'); | ||
@@ -20,0 +20,0 @@ // Save log to default log file "default.log". |
// Import. | ||
var log = require('./app'); | ||
var fs = require('fs'); | ||
const log = require('./app'); | ||
const fs = require('fs'); | ||
@@ -5,0 +5,0 @@ // Log data. |
4774
6.42%5
25%1
Infinity%66
-12%