Add Loguix

This commit is contained in:
Raphael 2024-01-13 16:09:29 +01:00
parent ca6ec930dc
commit 3ef3129b4d
3 changed files with 106 additions and 50 deletions

View File

@ -5,7 +5,13 @@
## Utilisation ## Utilisation
> - Dès que vous instanciez le package, un dossier `./logs` est crée dans lequels les fichiers journaux vont s'ajouter > - Dès que vous instanciez le package, configurez une seul fois le dossier de destination des logs
```js
const log = require("loguix")
log.setup("./logs")
```
### Fonctions principales ### Fonctions principales

134
main.js
View File

@ -6,53 +6,86 @@ const fs = require("fs")
const path = require("path") const path = require("path")
const basic = require("./basics") const basic = require("./basics")
setup() var isSetup = false
var logInstance = new Map()
var logStream = null
/** /**
* Configuration of Loguix * Configuration du module
* @param {string} logsDir
* @param {string} packageJsonPath
*/ */
function setup() { module.exports.setup = function(logsDir, packageJsonPath) {
if(!fs.existsSync(__dirname + path.sep + "logs" + path.sep)) { if(!isSetup) {
fs.mkdir(__dirname + path.sep + "logs", (err) => {
if(!err) { if(!logsDir) {
return new Error("[Loguix] - Error in Setup : MISS_LOGS_DIR")
}
if(!fs.existsSync(logsDir + path.sep)) {
fs.mkdir(logsDir, (err) => {
if(!err) {
console.log("[Logs] - Dossier de logs crée ! !") console.log("[Logs] - Dossier de logs crée ! !")
} else { } else {
console.log("[Logs] - Erreur d'écriture par manque de permission ") console.log("[Logs] - Erreur d'écriture par manque de permission ")
console.log(err) console.log(err)
} }
}) })
}
logStream = fs.createWriteStream(logsDir + path.sep + basic.getDate(true) + ".log", {
flags: 'a'
});
var nameOfPackage = "Unknown"
var versionOfPackage = "-.-.-"
try {
const packageJson = fs.readFileSync(packageJsonPath)
const packageJsonParsed = JSON.parse(packageJson)
nameOfPackage = packageJsonParsed.name
versionOfPackage = packageJsonParsed.version
} catch (error) {
console.log("[Loguix] - Error in Setup : INVALID_PACKAGE_JSON")
console.log(error)
}
const packageJson = fs.readFileSync(packageJsonPath)
logStream.write("[" + nameOfPackage + "@"+ versionOfPackage + "] - [" + basic.getDate(true) + "]" + "\n")
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()
});
isSetup = true
} else {
console.log("Loguix is already configure")
} }
logStream = fs.createWriteStream(__dirname + path.sep + "logs" + path.sep + basic.getDate(true) + ".log", {
flags: 'a'
});
process.config.variables.
logStream.write("[" + npm_package_name + "@"+ npm_package_version + "] - [" + basic.getDate(true) + "]" + "\n")
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()
});
@ -76,7 +109,7 @@ module.exports.LogType = class {
var logtext = "[" + this.type + "] - [INFO] - " + txt var logtext = "[" + this.type + "] - [INFO] - " + txt
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
@ -85,7 +118,7 @@ module.exports.LogType = class {
warn(txt) { warn(txt) {
var logtext = "[" + this.type + "] - [WARN] - " + txt var logtext = "[" + this.type + "] - [WARN] - " + txt
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
} }
@ -94,7 +127,7 @@ module.exports.LogType = class {
var logtext = "[" + this.type + "] - [ERROR] - " + txt var logtext = "[" + this.type + "] - [ERROR] - " + txt
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
@ -110,7 +143,7 @@ module.exports.LogType = class {
init: function(id, desc) { init: function(id, desc) {
parent.steps.set(id, desc) parent.steps.set(id, desc)
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + desc + " - En cours ..." var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + desc + " - En cours ..."
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
}, },
@ -119,13 +152,13 @@ module.exports.LogType = class {
if(parent.steps.has(id)) { if(parent.steps.has(id)) {
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + parent.steps.get(id) + " - Terminé !" var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + parent.steps.get(id) + " - Terminé !"
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
parent.steps.delete(id) parent.steps.delete(id)
} else { } else {
var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !" var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !"
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
} }
@ -136,13 +169,13 @@ module.exports.LogType = class {
if(parent.steps.has(id)) { 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 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") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
parent.steps.delete(id) parent.steps.delete(id)
} else { } else {
var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !" var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !"
logStream.write("[" + getDate() + "] - " + logtext + "\n") logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.log(logtext) console.log(logtext)
} }
@ -153,3 +186,20 @@ module.exports.LogType = class {
} }
} }
module.exports.getInstance = function (name) {
if(logInstance.has(name)) {
return logInstance.get(name)
} else {
var logtext = "[Logs] - [ERROR] - '" + name + "' n'est pas enregistré en tant qu'instance de log !"
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
console.error(logtext)
return null
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "loguix", "name": "loguix",
"version": "1.2.0", "version": "1.4.1",
"description": "Systeme de journaux simplifié", "description": "Systeme de journaux simplifié",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {