2022-08-13 12:18:38 +00:00
|
|
|
//Loguix - Système de log simplifié
|
|
|
|
//Concu par Raphael PICOT - Raphix
|
|
|
|
//GITLAB : https://gitlab.com/raphixscrap/loguix
|
|
|
|
|
|
|
|
|
|
|
|
const fs = require("fs")
|
|
|
|
var date = new Date()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// [Date Format] - Format de la date
|
|
|
|
|
|
|
|
var gmonth = date.getMonth()
|
|
|
|
var gday = date.getDate()
|
|
|
|
var gHour = date.getHours()
|
|
|
|
var gMinute = date.getMinutes()
|
|
|
|
var gSecondes = date.getSeconds()
|
|
|
|
|
|
|
|
|
|
|
|
if(date.getMonth() + 1 <= 9) {
|
|
|
|
gmonth = "0" + (date.getMonth() + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if(date.getDate() + 1 <= 9) {
|
|
|
|
gday = "0" + date.getDate()
|
|
|
|
}
|
|
|
|
|
|
|
|
if(date.getHours() + 1 <= 9) {
|
|
|
|
gHour = "0" + date.getHours()
|
|
|
|
}
|
|
|
|
|
|
|
|
if(date.getMinutes() + 1 <= 9) {
|
|
|
|
gMinute = "0" + date.getMinutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
if(date.getSeconds() + 1 <= 9) {
|
|
|
|
gSecondes = "0" + date.getSeconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentDate = date.getFullYear() + "-" + gmonth + "-" + gday + "-" + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
|
|
|
|
var returnDate = currentDate
|
|
|
|
|
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
// [Loguix Init Phase] - Settings for LogSystem
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
var env_name = null
|
|
|
|
var env_set = false
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
if(!fs.existsSync("logs/")) {
|
|
|
|
fs.mkdir("logs", (err) => {
|
|
|
|
if(err) {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
console.log("[Loguix] - Error on initialization phase : #E : LOGS_DIR_ERROR")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var logStream = fs.createWriteStream("logs/" + currentDate + ".log", {
|
|
|
|
flags: 'a'
|
|
|
|
});
|
|
|
|
|
|
|
|
var sys = {
|
2022-08-13 17:05:19 +00:00
|
|
|
"name":"Loguix",
|
2022-08-13 17:05:56 +00:00
|
|
|
"version":"1.0.0"
|
2022-08-13 12:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
// [Loguix Start Phase] - Start Function - Check state
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
logStream.write("[" + sys.name + "@" + sys.version + "] - [" + currentDate + "]" + "\n")
|
2022-08-13 17:05:19 +00:00
|
|
|
logStream.write("Loguix by Raphix" + "\n")
|
2022-08-13 12:18:38 +00:00
|
|
|
logStream.write("----------------------------------------------------------------" + "\n")
|
|
|
|
|
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
// [Function] - Get Loguix Date Format
|
|
|
|
|
|
|
|
module.exports.getLogFormatDate = () => {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
return returnDate
|
2022-08-13 12:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
// [Function] - Environnement Settings
|
|
|
|
|
|
|
|
module.exports.setName = (name) => {
|
|
|
|
env_name = name
|
|
|
|
env_set = true
|
|
|
|
|
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
// [Function] - Write into console with priority and write in log file
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
module.exports.write = (level, msg) => {
|
2022-08-13 17:05:19 +00:00
|
|
|
if(env_set == true) {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
if(level == "warn") {
|
2022-08-13 17:05:19 +00:00
|
|
|
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
|
|
|
|
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
|
2022-08-13 12:18:38 +00:00
|
|
|
logStream.write(message);
|
|
|
|
console.warn(consoleMessage)
|
|
|
|
|
|
|
|
} else if(level == "error") {
|
2022-08-13 17:05:19 +00:00
|
|
|
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
|
|
|
|
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
|
2022-08-13 12:18:38 +00:00
|
|
|
logStream.write(message);
|
|
|
|
console.error(consoleMessage)
|
|
|
|
|
|
|
|
} else if(level == "info") {
|
2022-08-13 17:05:19 +00:00
|
|
|
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
|
|
|
|
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
|
2022-08-13 12:18:38 +00:00
|
|
|
logStream.write(message);
|
|
|
|
console.log(consoleMessage)
|
|
|
|
|
|
|
|
} else if(level == "fatal") {
|
2022-08-13 17:05:19 +00:00
|
|
|
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
|
|
|
|
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
|
2022-08-13 12:18:38 +00:00
|
|
|
logStream.write(message);
|
|
|
|
console.log(consoleMessage)
|
|
|
|
} else {
|
2022-08-13 17:05:19 +00:00
|
|
|
logStream.write("[" + currentDate + "] - " + "[Loguix] - Write - Level is not reconized\n")
|
2022-08-13 12:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
} else {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
console.error("[Loguix] - Name is not configure.");
|
|
|
|
|
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
// [Function] - Fatal Error - End programm when exit
|
|
|
|
|
|
|
|
module.exports.stop = (endCode) => {
|
|
|
|
|
|
|
|
process.on('uncaughtException', () => {
|
|
|
|
logStream.end( "["+ endCode + "]" + " - [END OF LOGS] - [" + currentDate + ']')
|
|
|
|
console.log("check")
|
|
|
|
logStream.close()
|
|
|
|
});
|
|
|
|
|
|
|
|
nonexistentFunc();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err) => {
|
2022-08-13 17:05:19 +00:00
|
|
|
logStream.write("[" + currentDate + "] - ["+ "FATAL" + "] - [" + env_name + "]" + " The programm has encountered an error ! Please Restart ! #E = " + err + "\n")
|
2022-08-13 12:18:38 +00:00
|
|
|
logStream.end( "["+ "UNCAUGHT_EXCEPTION" + "]" + " - [END OF LOGS] - [" + currentDate + ']')
|
|
|
|
logStream.close()
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('beforeExit', () => {
|
|
|
|
logStream.end( "["+ "NORMAL_END" + "]" + " - [END OF LOGS] - [" + currentDate + ']')
|
|
|
|
logStream.close()
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//[Sandbox] - Test
|