Add Loguix
This commit is contained in:
parent
ca6ec930dc
commit
3ef3129b4d
@ -5,7 +5,13 @@
|
||||
|
||||
## 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
|
||||
|
||||
|
134
main.js
134
main.js
@ -6,53 +6,86 @@ const fs = require("fs")
|
||||
const path = require("path")
|
||||
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)) {
|
||||
fs.mkdir(__dirname + path.sep + "logs", (err) => {
|
||||
if(!err) {
|
||||
if(!isSetup) {
|
||||
|
||||
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(err)
|
||||
}
|
||||
console.log("[Logs] - Erreur d'écriture par manque de permission ")
|
||||
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
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
|
||||
@ -85,7 +118,7 @@ module.exports.LogType = class {
|
||||
warn(txt) {
|
||||
|
||||
var logtext = "[" + this.type + "] - [WARN] - " + txt
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
}
|
||||
@ -94,7 +127,7 @@ module.exports.LogType = class {
|
||||
|
||||
|
||||
var logtext = "[" + this.type + "] - [ERROR] - " + txt
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
|
||||
@ -110,7 +143,7 @@ module.exports.LogType = class {
|
||||
init: function(id, desc) {
|
||||
parent.steps.set(id, desc)
|
||||
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + desc + " - En cours ..."
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
},
|
||||
@ -119,13 +152,13 @@ module.exports.LogType = class {
|
||||
if(parent.steps.has(id)) {
|
||||
|
||||
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)
|
||||
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")
|
||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
}
|
||||
@ -136,13 +169,13 @@ module.exports.LogType = class {
|
||||
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")
|
||||
logStream.write("[" + basic.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")
|
||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "loguix",
|
||||
"version": "1.2.0",
|
||||
"version": "1.4.1",
|
||||
"description": "Systeme de journaux simplifié",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user