futurosenso-log
Advanced tools
Comparing version 1.1.2 to 1.2.0
48
log.js
@@ -6,2 +6,3 @@ const fs = require('fs'); | ||
const consoleLog = process.stdout ? process.stdout : null; | ||
const consoleError = process.stderr ? process.stderr : null; | ||
var startDate = new Date(); | ||
@@ -68,3 +69,26 @@ const logDir = "./logs"; | ||
exports.log = function(data, currentDate) { | ||
exports.forkStdOut = function() { | ||
console.log = function (d) { | ||
exports.log(d); | ||
}; | ||
} | ||
exports.forkStdError = function() { | ||
console.error = function(d) { | ||
exports.log(d, undefined, true); | ||
}; | ||
} | ||
exports.redirectStdError = function() { | ||
process.stderr.write = function(d) { | ||
if(d) { | ||
if (d.endsWith("\n")) | ||
d.split(d.length - 1, 1); | ||
exports.log(d, undefined, true, true); | ||
} | ||
}; | ||
} | ||
exports.log = function(data, currentDate, isError, isRedirect) { | ||
if(logFile==undefined) | ||
@@ -105,11 +129,23 @@ selectLogFile(startDate); | ||
// WRITE FILE | ||
let result = fs.writeSync(logFile, util.format("%s:%s:%s.%s GMT - %s\n", hour, min, sec, milli, data)); | ||
let result; | ||
if(isError) | ||
result = fs.writeSync(logFile, util.format("%s:%s:%s.%s GMT - stderr - %s\n", hour, min, sec, milli, data)); | ||
else | ||
result = fs.writeSync(logFile, util.format("%s:%s:%s.%s GMT - %s\n", hour, min, sec, milli, data)); | ||
if(!result || result < 1) { | ||
if(consoleLog != null) | ||
consoleLog.write(util.format("%s %s\n", "Error trying to write in ", logFile)); | ||
if(!isRedirect && isError && consoleError != null) | ||
consoleError.write(util.format("%s:%s:%s.%s GMT: %s\n", hour, min, sec, milli, "Error trying to write in " + logFile)); | ||
else if(consoleLog != null) | ||
consoleLog.write(util.format("%s:%s:%s.%s GMT: %s\n", hour, min, sec, milli, "Error trying to write in " + logFile)); | ||
} | ||
// WRITE LOG | ||
if(consoleLog != null) { | ||
consoleLog.write(util.format("%s:%s:%s.%s GMT: %s\n", hour, min, sec, milli, data)) ; | ||
if(!isRedirect && isError && consoleError != null) | ||
consoleError.write(util.format("%s:%s:%s.%s GMT: %s\n", hour, min, sec, milli, data)) ; | ||
else if(consoleLog != null) { | ||
if(isRedirect) | ||
consoleLog.write(util.format("%s:%s:%s.%s GMT: stderr: %s\n", hour, min, sec, milli, data)) ; | ||
else | ||
consoleLog.write(util.format("%s:%s:%s.%s GMT: %s\n", hour, min, sec, milli, data)) ; | ||
} | ||
}; |
103
log.test.js
@@ -5,27 +5,23 @@ const util = require("util"); | ||
const logger = require("./log"); | ||
var path; | ||
test("Test logging lines in file", () => { | ||
let date = new Date(); | ||
let year = date.getUTCFullYear(); | ||
let month = date.getUTCMonth() + 1; | ||
let day = date.getUTCDate(); | ||
if (month < 10) { | ||
month = "0" + month; | ||
} | ||
if (day < 10) { | ||
day = "0" + day; | ||
} | ||
let date = new Date(); | ||
let year = date.getUTCFullYear(); | ||
let month = date.getUTCMonth() + 1; | ||
let day = date.getUTCDate(); | ||
if (month < 10) { | ||
month = "0" + month; | ||
} | ||
if (day < 10) { | ||
day = "0" + day; | ||
} | ||
path = util.format("logs/%s/%s/%s-%s-%s.log", year, month, year, month, day); | ||
var path = util.format("logs/%s/%s/%s-%s-%s.log", year, month, year, month, day); | ||
if (fs.existsSync(path)) | ||
fs.unlinkSync(path); | ||
if (fs.existsSync(path)) | ||
fs.unlinkSync(path); | ||
//logger.init(); | ||
test("Test logging lines in file", () => { | ||
logger.log("This is just a first test line"); | ||
logger.log("This is just a second test line"); | ||
logger.log(path); | ||
expect(fs.existsSync(path)).toBeTruthy(); | ||
@@ -58,1 +54,72 @@ }); | ||
}); | ||
test("Test fork standard output", (done) => { | ||
logger.forkStdOut(); | ||
console.log("Forking test to stdout and log file"); | ||
var matching = /\d\d:\d\d:\d\d\.\d\d\d GMT - Forking test to stdout and log file/; | ||
let myInterface = readline.createInterface( | ||
{ | ||
input: fs.createReadStream(path) | ||
}); | ||
var index = 0; | ||
myInterface.on("line", function (line) { | ||
index++; | ||
switch (index) { | ||
case 4: expect(line).toMatch(matching); logger.log("fork stdout - ok, line matching"); break; | ||
default: expect(line).toBeDefined(); break; | ||
} | ||
}); | ||
myInterface.on("close", function (line) { | ||
expect(index).toBeGreaterThanOrEqual(4); | ||
done(); | ||
}); | ||
}); | ||
test("Test fork standard error", (done) => { | ||
logger.forkStdError(); | ||
console.error("Forking test to stderr and log file"); | ||
var matching = /\d\d:\d\d:\d\d\.\d\d\d GMT - stderr - Forking test to stderr and log file/; | ||
let myInterface = readline.createInterface( | ||
{ | ||
input: fs.createReadStream(path) | ||
}); | ||
var index = 0; | ||
myInterface.on("line", function (line) { | ||
index++; | ||
switch (index) { | ||
case 6: expect(line).toMatch(matching); logger.log("fork stderr - ok, line matching"); break; | ||
default: expect(line).toBeDefined(); break; | ||
} | ||
}); | ||
myInterface.on("close", function (line) { | ||
expect(index).toBeGreaterThanOrEqual(6); | ||
done(); | ||
}); | ||
}); | ||
test("Test redirect standard error", (done) => { | ||
logger.redirectStdError(); | ||
process.stderr.write("Redirection test to stdout and log file"); | ||
var matching = /\d\d:\d\d:\d\d\.\d\d\d GMT - stderr - Redirection test to stdout and log file/; | ||
let myInterface = readline.createInterface( | ||
{ | ||
input: fs.createReadStream(path) | ||
}); | ||
var index = 0; | ||
myInterface.on("line", function (line) { | ||
index++; | ||
switch (index) { | ||
case 8: expect(line).toMatch(matching); logger.log("redirect stderr - ok, line matching"); break; | ||
default: expect(line).toBeDefined(); break; | ||
} | ||
}); | ||
myInterface.on("close", function (line) { | ||
expect(index).toBeGreaterThanOrEqual(8); | ||
done(); | ||
}); | ||
}); |
@@ -5,4 +5,4 @@ { | ||
"email": "dev@futurosenso.com", | ||
"version": "1.1.2", | ||
"description": "Small logging utility. You get a logger that adds a log to a file and to the console. A new file is created if the day has changed. The log files are organized in folders by year and month.", | ||
"version": "1.2.0", | ||
"description": "Logging utility. You get a logger that adds a log to a file and to the console. A new file is created if the day has changed. The log files are organized in folders by year and month.", | ||
"license": "ISC", | ||
@@ -13,5 +13,8 @@ "keywords": [ | ||
"logging", | ||
"error log", | ||
"util", | ||
"server", | ||
"console", | ||
"stdout", | ||
"stderr", | ||
"synchronized" | ||
@@ -18,0 +21,0 @@ ], |
@@ -38,1 +38,21 @@ #### Small logging utility | ||
logs/2019/03/2019-03-06.log | ||
#### Forking of standard output 'stdout' and standard error 'stderr' | ||
logger.forkStdOut(); | ||
After calling logger.forkStdOut(), every message passed to console.log() will also be added to the log file. | ||
logger.forkStdError(); | ||
After calling logger.forkStdError(), every message passed to console.error() will also be added to the log file. | ||
14:06:52.763 GMT - stderr - forking test to stderr and log file | ||
#### Redirection of process.stderr.write | ||
logger.redirectStdError(); | ||
Every call to process.stderr.write will be directly redirected to the log file and the console standard output. | ||
14:06:52.763 GMT - stderr - redirection test to stderr.write |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17621
368
57