Compare commits

..

2 Commits

Author SHA1 Message Date
0e22cc935a Add Loguix 2024-01-13 16:09:37 +01:00
3ef3129b4d Add Loguix 2024-01-13 16:09:29 +01:00
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

82
main.js
View File

@ -6,15 +6,25 @@ 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(!logsDir) {
return new Error("[Loguix] - Error in Setup : MISS_LOGS_DIR")
}
if(!fs.existsSync(logsDir + path.sep)) {
fs.mkdir(logsDir, (err) => {
if(!err) { if(!err) {
@ -30,12 +40,26 @@ function setup() {
} }
logStream = fs.createWriteStream(__dirname + path.sep + "logs" + path.sep + basic.getDate(true) + ".log", { logStream = fs.createWriteStream(logsDir + path.sep + basic.getDate(true) + ".log", {
flags: 'a' flags: 'a'
}); });
process.config.variables. var nameOfPackage = "Unknown"
logStream.write("[" + npm_package_name + "@"+ npm_package_version + "] - [" + basic.getDate(true) + "]" + "\n") 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") logStream.write("----------------------------------------------------------------" + "\n")
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
@ -54,6 +78,15 @@ function setup() {
}); });
isSetup = true
} else {
console.log("Loguix is already configure")
}
} }
@ -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.2",
"description": "Systeme de journaux simplifié", "description": "Systeme de journaux simplifié",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {