2022-08-13 12:18:38 +00:00
|
|
|
//Loguix - Système de log simplifié
|
|
|
|
//Concu par Raphael PICOT - Raphix
|
2023-09-23 08:53:38 +00:00
|
|
|
//GIT : https://git.raphix.fr/lib/loguix
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
const fs = require("fs")
|
2023-09-23 08:53:38 +00:00
|
|
|
const path = require("path")
|
|
|
|
const basic = require("./basics")
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
setup()
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-10-21 14:59:42 +00:00
|
|
|
/**
|
|
|
|
* Configuration of Loguix
|
|
|
|
*/
|
2023-09-23 08:53:38 +00:00
|
|
|
function setup() {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
if(!fs.existsSync(__dirname + path.sep + "logs" + path.sep)) {
|
|
|
|
fs.mkdir(__dirname + path.sep + "logs", (err) => {
|
|
|
|
if(!err) {
|
|
|
|
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
console.log("[Logs] - Dossier de logs crée ! !")
|
|
|
|
|
|
|
|
} else {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-10-21 14:59:42 +00:00
|
|
|
console.log("[Logs] - Erreur d'écriture par manque de permission ")
|
2023-09-23 08:53:38 +00:00
|
|
|
console.log(err)
|
|
|
|
}
|
2022-08-13 17:05:19 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
})
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
}
|
2022-08-13 17:05:19 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
logStream = fs.createWriteStream(__dirname + path.sep + "logs" + path.sep + basic.getDate(true) + ".log", {
|
|
|
|
flags: 'a'
|
|
|
|
});
|
|
|
|
|
2023-10-21 14:59:42 +00:00
|
|
|
process.config.variables.
|
|
|
|
logStream.write("[" + npm_package_name + "@"+ npm_package_version + "] - [" + basic.getDate(true) + "]" + "\n")
|
2023-09-23 08:53:38 +00:00
|
|
|
logStream.write("----------------------------------------------------------------" + "\n")
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err) => {
|
|
|
|
console.error("["+ "FATAL" + "] - The application has encountered an error ! Please Restart ! #E = " + err + "\n")
|
|
|
|
console.error(err)
|
|
|
|
logStream.write("[" + basic.getDate() + "] - ["+ "FATAL" + "] - The application has encountered an error ! Please Restart ! #E = " + err + "\n")
|
|
|
|
logStream.end( "["+ "UNCAUGHT_EXCEPTION" + "]" + " - [END OF LOGS] - [" + basic.getDate() + ']')
|
|
|
|
logStream.close()
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
process.on('beforeExit', (err) => {
|
|
|
|
logStream.end( "["+ "BEFORE_EXIT" + "]" + " - [END OF LOGS] - [" + basic.getDate(true) + ']')
|
|
|
|
logStream.close()
|
|
|
|
|
|
|
|
});
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
|
|
|
|
|
2022-08-13 12:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
/**
|
|
|
|
* Déclare une nouvelle instance de log
|
|
|
|
* @param {string} typeName Indique le nom du type de log
|
|
|
|
*/
|
|
|
|
module.exports.LogType = class {
|
|
|
|
|
|
|
|
constructor(typeName) {
|
|
|
|
this.type = typeName;
|
|
|
|
this.steps = new Map()
|
|
|
|
this.step = this.initializeStep()
|
|
|
|
logInstance.set(typeName, this)
|
|
|
|
}
|
2022-08-13 17:05:19 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
log(txt) {
|
2022-08-13 17:05:19 +00:00
|
|
|
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
var logtext = "[" + this.type + "] - [INFO] - " + txt
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
|
|
|
|
|
2022-08-13 17:05:19 +00:00
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
warn(txt) {
|
|
|
|
|
|
|
|
var logtext = "[" + this.type + "] - [WARN] - " + txt
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
|
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
error(txt) {
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
var logtext = "[" + this.type + "] - [ERROR] - " + txt
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
|
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
initializeStep() {
|
|
|
|
const parent = this;
|
|
|
|
return {
|
|
|
|
init: function(id, desc) {
|
|
|
|
parent.steps.set(id, desc)
|
|
|
|
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + desc + " - En cours ..."
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
|
|
|
|
},
|
|
|
|
end: function(id) {
|
|
|
|
|
|
|
|
if(parent.steps.has(id)) {
|
|
|
|
|
|
|
|
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + parent.steps.get(id) + " - Terminé !"
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
parent.steps.delete(id)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !"
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
error: function(id, errorDesc) {
|
|
|
|
|
|
|
|
if(parent.steps.has(id)) {
|
|
|
|
|
|
|
|
var logtext = "[" + parent.type + "] - [ERROR] - [STEP] - " + parent.steps.get(id) + " - Une erreur a été rencontré dans l'étape ! : #E = " + errorDesc
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
parent.steps.delete(id)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !"
|
|
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
|
|
console.log(logtext)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-08-13 12:18:38 +00:00
|
|
|
|
2023-09-23 08:53:38 +00:00
|
|
|
}
|