Version 1.1.0 - Change all methods

This commit is contained in:
CICD - Pipeline 2023-09-23 10:53:38 +02:00
parent 1078c7d2a5
commit 33fea0cae2
4 changed files with 199 additions and 141 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
logs
*.log
old-version.js
nodes_modules

65
basics.js Normal file
View File

@ -0,0 +1,65 @@
const fs = require("fs")
const path = require("path")
module.exports.getDate = function (formated) {
var date = new Date()
// [Date Format] - Format de la date
var gmonth = date.getMonth()
var gday = date.getDate()
var gHour = date.getHours()
var gMinute = date.getMinutes()
var gSecondes = date.getSeconds()
if(date.getMonth() + 1 <= 10) {
gmonth = "0" + (date.getMonth() + 1)
}
if(date.getDate() + 1 <= 10) {
gday = "0" + date.getDate()
}
if(date.getHours() + 1 <= 10) {
gHour = "0" + date.getHours()
}
if(date.getMinutes() + 1 <= 10) {
gMinute = "0" + date.getMinutes()
}
if(date.getSeconds() + 1 <= 10) {
gSecondes = "0" + date.getSeconds()
}
if(!formated) {
return gday + "/" + gmonth + " - " + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
} else {
return date.getFullYear() + "-" + gmonth + "-" + gday + "-" + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
}
}
module.exports.checkLogsDirectory = () => {
if(!fs.existsSync(__dirname + path.sep + "logs" + path.sep)) {
fs.mkdir(__dirname + path.sep + "logs", (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)
}
})
}
}

273
main.js
View File

@ -1,161 +1,154 @@
//Loguix - Système de log simplifié //Loguix - Système de log simplifié
//Concu par Raphael PICOT - Raphix //Concu par Raphael PICOT - Raphix
//GITLAB : https://gitlab.com/raphixscrap/loguix //GIT : https://git.raphix.fr/lib/loguix
const fs = require("fs") const fs = require("fs")
var date = new Date() const path = require("path")
const basic = require("./basics")
setup()
function setup() {
if(!fs.existsSync(__dirname + path.sep + "logs" + path.sep)) {
fs.mkdir(__dirname + path.sep + "logs", (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(__dirname + path.sep + "logs" + path.sep + basic.getDate(true) + ".log", {
flags: 'a'
});
logStream.write("[" + require("../../package.json").name + "@"+ require("../../package.json").version + "] - [" + basic.getDate(true) + "]" + "\n")
logStream.write("Subsonics-Web by Raphix" + "\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()
});
// [Date Format] - Format de la date
var gmonth = date.getMonth()
var gday = date.getDate()
var gHour = date.getHours()
var gMinute = date.getMinutes()
var gSecondes = date.getSeconds()
if(date.getMonth() + 1 <= 9) {
gmonth = "0" + (date.getMonth() + 1)
} }
if(date.getDate() + 1 <= 9) { /**
gday = "0" + date.getDate() * Déclare une nouvelle instance de log
} * @param {string} typeName Indique le nom du type de log
*/
module.exports.LogType = class {
if(date.getHours() + 1 <= 9) { constructor(typeName) {
gHour = "0" + date.getHours() this.type = typeName;
} this.steps = new Map()
this.step = this.initializeStep()
logInstance.set(typeName, this)
}
if(date.getMinutes() + 1 <= 9) { log(txt) {
gMinute = "0" + date.getMinutes()
}
if(date.getSeconds() + 1 <= 9) {
gSecondes = "0" + date.getSeconds()
}
var currentDate = date.getFullYear() + "-" + gmonth + "-" + gday + "-" + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
var returnDate = currentDate
// [Loguix Init Phase] - Settings for LogSystem
var env_name = null var logtext = "[" + this.type + "] - [INFO] - " + txt
var env_set = false logStream.write("[" + getDate() + "] - " + logtext + "\n")
console.log(logtext)
if(!fs.existsSync("logs/")) {
fs.mkdir("logs", (err) => {
if(err) {
console.log("[Loguix] - Error on initialization phase : #E : LOGS_DIR_ERROR") }
warn(txt) {
var logtext = "[" + this.type + "] - [WARN] - " + txt
logStream.write("[" + getDate() + "] - " + logtext + "\n")
console.log(logtext)
}
error(txt) {
var logtext = "[" + this.type + "] - [ERROR] - " + txt
logStream.write("[" + getDate() + "] - " + logtext + "\n")
console.log(logtext)
}
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)
}
}
} }
})
}
var logStream = fs.createWriteStream("logs/" + currentDate + ".log", {
flags: 'a'
});
var sys = {
"name":"Loguix",
"version":"1.0.0"
}
// [Loguix Start Phase] - Start Function - Check state
logStream.write("[" + sys.name + "@" + sys.version + "] - [" + currentDate + "]" + "\n")
logStream.write("Loguix by Raphix" + "\n")
logStream.write("----------------------------------------------------------------" + "\n")
// [Function] - Get Loguix Date Format
module.exports.getLogFormatDate = () => {
return returnDate
}
// [Function] - Environnement Settings
module.exports.setName = (name) => {
env_name = name
env_set = true
}
// [Function] - Write into console with priority and write in log file
module.exports.write = (level, msg) => {
if(env_set == true) {
if(level == "warn") {
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
logStream.write(message);
console.warn(consoleMessage)
} else if(level == "error") {
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
logStream.write(message);
console.error(consoleMessage)
} else if(level == "info") {
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
logStream.write(message);
console.log(consoleMessage)
} else if(level == "fatal") {
var message = "[" + currentDate + "] - [" + env_name + "]" + "["+ level.toUpperCase() + "] - " + msg + "\n";
var consoleMessage = "[" + currentDate + "] - [" + env_name + "]" + " - ["+ level.toUpperCase() + "] - " + msg
logStream.write(message);
console.log(consoleMessage)
} else {
logStream.write("[" + currentDate + "] - " + "[Loguix] - Write - Level is not reconized\n")
}
} else {
console.error("[Loguix] - Name is not configure.");
} }
} }
// [Function] - Fatal Error - End programm when exit
module.exports.stop = (endCode) => {
process.on('uncaughtException', () => {
logStream.end( "["+ endCode + "]" + " - [END OF LOGS] - [" + currentDate + ']')
console.log("check")
logStream.close()
});
nonexistentFunc();
}
process.on('uncaughtException', (err) => {
logStream.write("[" + currentDate + "] - ["+ "FATAL" + "] - [" + env_name + "]" + " The programm has encountered an error ! Please Restart ! #E = " + err + "\n")
logStream.end( "["+ "UNCAUGHT_EXCEPTION" + "]" + " - [END OF LOGS] - [" + currentDate + ']')
logStream.close()
});
process.on('beforeExit', () => {
logStream.end( "["+ "NORMAL_END" + "]" + " - [END OF LOGS] - [" + currentDate + ']')
logStream.close()
});
//[Sandbox] - Test

View File

@ -1,20 +1,16 @@
{ {
"name": "loguix", "name": "loguix",
"version": "1.0.0", "version": "1.1.0",
"description": "Log System for JS", "description": "Log System for JS",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"debug": "node test.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://gitlab.com/raphixscrap/loguix.git" "url": "git+https://git.raphix.fr/lib/loguix.git"
}, },
"keywords": [], "keywords": [],
"author": "Raphix", "author": "Raphix",
"license": "ISC", "license": "ISC"
"bugs": {
"url": "https://gitlab.com/raphixscrap/loguix/issues"
},
"homepage": "https://gitlab.com/raphixscrap/loguix#readme"
} }