Compare commits
No commits in common. "0e22cc935ad4b597acb0ea89d38a267ce9b21384" and "ca6ec930dca435ad24995976934869ab1d4021fe" have entirely different histories.
0e22cc935a
...
ca6ec930dc
@ -5,13 +5,7 @@
|
|||||||
|
|
||||||
## Utilisation
|
## Utilisation
|
||||||
|
|
||||||
> - Dès que vous instanciez le package, configurez une seul fois le dossier de destination des logs
|
> - Dès que vous instanciez le package, un dossier `./logs` est crée dans lequels les fichiers journaux vont s'ajouter
|
||||||
|
|
||||||
```js
|
|
||||||
const log = require("loguix")
|
|
||||||
log.setup("./logs")
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### Fonctions principales
|
### Fonctions principales
|
||||||
|
|
||||||
|
142
main.js
142
main.js
@ -6,87 +6,54 @@ const fs = require("fs")
|
|||||||
const path = require("path")
|
const path = require("path")
|
||||||
const basic = require("./basics")
|
const basic = require("./basics")
|
||||||
|
|
||||||
var isSetup = false
|
setup()
|
||||||
var logInstance = new Map()
|
|
||||||
var logStream = null
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration du module
|
* Configuration of Loguix
|
||||||
* @param {string} logsDir
|
|
||||||
* @param {string} packageJsonPath
|
|
||||||
*/
|
*/
|
||||||
module.exports.setup = function(logsDir, packageJsonPath) {
|
function setup() {
|
||||||
|
|
||||||
if(!isSetup) {
|
if(!fs.existsSync(__dirname + path.sep + "logs" + path.sep)) {
|
||||||
|
fs.mkdir(__dirname + path.sep + "logs", (err) => {
|
||||||
|
if(!err) {
|
||||||
|
|
||||||
|
|
||||||
if(!logsDir) {
|
console.log("[Logs] - Dossier de logs crée ! !")
|
||||||
return new Error("[Loguix] - Error in Setup : MISS_LOGS_DIR")
|
|
||||||
|
} else {
|
||||||
}
|
|
||||||
if(!fs.existsSync(logsDir + path.sep)) {
|
|
||||||
fs.mkdir(logsDir, (err) => {
|
|
||||||
if(!err) {
|
|
||||||
|
|
||||||
|
|
||||||
console.log("[Logs] - Dossier de logs crée ! !")
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
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"
|
console.log("[Logs] - Erreur d'écriture par manque de permission ")
|
||||||
var versionOfPackage = "-.-.-"
|
console.log(err)
|
||||||
|
}
|
||||||
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()
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -109,7 +76,7 @@ module.exports.LogType = class {
|
|||||||
|
|
||||||
|
|
||||||
var logtext = "[" + this.type + "] - [INFO] - " + txt
|
var logtext = "[" + this.type + "] - [INFO] - " + txt
|
||||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||||
console.log(logtext)
|
console.log(logtext)
|
||||||
|
|
||||||
|
|
||||||
@ -118,7 +85,7 @@ module.exports.LogType = class {
|
|||||||
warn(txt) {
|
warn(txt) {
|
||||||
|
|
||||||
var logtext = "[" + this.type + "] - [WARN] - " + txt
|
var logtext = "[" + this.type + "] - [WARN] - " + txt
|
||||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||||
console.log(logtext)
|
console.log(logtext)
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -127,7 +94,7 @@ module.exports.LogType = class {
|
|||||||
|
|
||||||
|
|
||||||
var logtext = "[" + this.type + "] - [ERROR] - " + txt
|
var logtext = "[" + this.type + "] - [ERROR] - " + txt
|
||||||
logStream.write("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||||
console.log(logtext)
|
console.log(logtext)
|
||||||
|
|
||||||
|
|
||||||
@ -143,7 +110,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("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||||
console.log(logtext)
|
console.log(logtext)
|
||||||
|
|
||||||
},
|
},
|
||||||
@ -152,13 +119,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("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + 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("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||||
console.log(logtext)
|
console.log(logtext)
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -169,13 +136,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("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + 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("[" + basic.getDate() + "] - " + logtext + "\n")
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||||
console.log(logtext)
|
console.log(logtext)
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -186,20 +153,3 @@ 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",
|
"name": "loguix",
|
||||||
"version": "1.4.2",
|
"version": "1.2.0",
|
||||||
"description": "Systeme de journaux simplifié",
|
"description": "Systeme de journaux simplifié",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user