log module with no-cli fallback
This commit is contained in:
parent
8ceedfca0e
commit
ba044c9e6d
|
@ -0,0 +1,96 @@
|
|||
var methods = {};
|
||||
let fs = require('fs');
|
||||
let util = require('util');
|
||||
|
||||
|
||||
// save new line to file
|
||||
newLine = (prefix, obj) => {
|
||||
let date = new Date(); // current date
|
||||
let filename = global['gds'].cfg.log.filename(); // filename
|
||||
let dir = global['gds'].cfg.log.directory(); // directory
|
||||
let path = dir + filename; // filepath
|
||||
let fs_options = { // fs options for encoding, file mode and file flag
|
||||
encoding: "utf8",
|
||||
flag: "a+" // Open file for reading and appending. The file is created if it does not exist.
|
||||
};
|
||||
let date_string =
|
||||
("0" + date.getUTCHours()).slice(-2) + ":" +
|
||||
("0" + date.getUTCMinutes()).slice(-2) + ":" +
|
||||
("0" + date.getUTCSeconds()).slice(-2);
|
||||
|
||||
// directory not existing; create directory
|
||||
if(!fs.existsSync(dir)) {
|
||||
fs.mkdir(dir, (err) => {
|
||||
if(err) throw err;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// type matching
|
||||
if(
|
||||
typeof obj == "string" ||
|
||||
typeof obj == "number" ||
|
||||
typeof obj == "boolean"
|
||||
) fs.appendFileSync(path, prefix + " " + date_string + " | " + obj + "\n", fs_options);
|
||||
else
|
||||
if(typeof obj == "object") {
|
||||
let lines = JSON.stringify(obj, null, '\t'); // prettyprint obj as JSON
|
||||
lines.split(/\r?\n/).forEach((line) => { // for each linebreak (\r | \n)
|
||||
fs.appendFileSync(path, prefix + " " + date_string + " | " + line + "\n", fs_options);
|
||||
});
|
||||
} else {
|
||||
// do nothing; not supported
|
||||
// typeof obj == "symbol"
|
||||
// typeof obj == "undefined"
|
||||
// typeof obj == "function"
|
||||
}
|
||||
};
|
||||
|
||||
fallback = (fn, ...data) => {
|
||||
if(data.length == 1) data = data[0];
|
||||
|
||||
fn.apply(null, data);
|
||||
}
|
||||
|
||||
|
||||
// LOG | INFO
|
||||
methods.log = (...data) => {
|
||||
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
|
||||
else fallback(console.log, data);
|
||||
|
||||
if(data.length == 1) data = data[0];
|
||||
newLine(" [LOG]", data);
|
||||
};
|
||||
methods.info = methods.log;
|
||||
|
||||
// WARNING
|
||||
methods.warn = (...data) => {
|
||||
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
|
||||
else fallback(console.warn, data);
|
||||
|
||||
if(data.length == 1) data = data[0];
|
||||
newLine(" [WARN]", data);
|
||||
};
|
||||
|
||||
// ERROR
|
||||
methods.error = (...data) => {
|
||||
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
|
||||
else fallback(console.error, data);
|
||||
|
||||
if(data.length == 1) data = data[0];
|
||||
newLine("[ERROR]", data);
|
||||
};
|
||||
methods.err = methods.error;
|
||||
|
||||
// DEBUG
|
||||
methods.debug = (...data) => {
|
||||
if(global['gds'].debug) {
|
||||
if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data);
|
||||
else fallback(console.log, data);
|
||||
|
||||
if(data.length == 1) data = data[0];
|
||||
newLine("[DEBUG]", data);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = methods;
|
Loading…
Reference in New Issue