1
0
Fork 0

logging update

This commit is contained in:
rxbn_ 2019-06-30 20:56:48 +02:00
parent 815f058ffe
commit c71b5cbbfa
3 changed files with 51 additions and 35 deletions

View File

@ -8,6 +8,12 @@
*/
module.exports = {
log: {
directory: global['__dirname'] + "/logs/",
filename: () => {
return (new Date()).toISOString().substr(0, 10) + ".log"
}
},
web: {
host: "auth.rxbn.de",
port: 8080,

View File

@ -10,6 +10,51 @@ 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"
}
}
// LOG | INFO
methods.log = (...data) => {
global['modules'].cli.log.apply(global['modules'].cli, data);
@ -46,39 +91,4 @@ methods.debug = (...data) => {
}
};
// save new line to file
newLine = (prefix, obj) => {
let date = new Date(); // current date
let filename = new Date().toISOString().substr(0, 10) + ".log"; // filename
let path = global['__dirname'] + "/logs/"+ filename; // filepath
let fs_options = { // fs options for encoding, file mode and file flag
encoding: "utf8",
flag: "a+"
};
let date_string =
("0" + date.getUTCHours()).slice(-2) + ":" +
("0" + date.getUTCMinutes()).slice(-2) + ":" +
("0" + date.getUTCSeconds()).slice(-2);
// 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"
}
}
module.exports = methods;

View File