Version 1.1.0 - Change all methods
This commit is contained in:
parent
1078c7d2a5
commit
33fea0cae2
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
logs
|
||||
*.log
|
||||
old-version.js
|
||||
nodes_modules
|
65
basics.js
Normal file
65
basics.js
Normal 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)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
259
main.js
259
main.js
@ -1,161 +1,154 @@
|
||||
//Loguix - Système de log simplifié
|
||||
//Concu par Raphael PICOT - Raphix
|
||||
//GITLAB : https://gitlab.com/raphixscrap/loguix
|
||||
|
||||
//GIT : https://git.raphix.fr/lib/loguix
|
||||
|
||||
const fs = require("fs")
|
||||
var date = new Date()
|
||||
const path = require("path")
|
||||
const basic = require("./basics")
|
||||
|
||||
|
||||
setup()
|
||||
|
||||
// [Date Format] - Format de la date
|
||||
function setup() {
|
||||
|
||||
var gmonth = date.getMonth()
|
||||
var gday = date.getDate()
|
||||
var gHour = date.getHours()
|
||||
var gMinute = date.getMinutes()
|
||||
var gSecondes = date.getSeconds()
|
||||
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 {
|
||||
|
||||
if(date.getMonth() + 1 <= 9) {
|
||||
gmonth = "0" + (date.getMonth() + 1)
|
||||
}
|
||||
console.log("[Logs] -Erreur d'écriture par manque de permission ")
|
||||
console.log(err)
|
||||
}
|
||||
|
||||
if(date.getDate() + 1 <= 9) {
|
||||
gday = "0" + date.getDate()
|
||||
}
|
||||
|
||||
if(date.getHours() + 1 <= 9) {
|
||||
gHour = "0" + date.getHours()
|
||||
}
|
||||
|
||||
if(date.getMinutes() + 1 <= 9) {
|
||||
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 env_set = false
|
||||
|
||||
if(!fs.existsSync("logs/")) {
|
||||
fs.mkdir("logs", (err) => {
|
||||
if(err) {
|
||||
|
||||
console.log("[Loguix] - Error on initialization phase : #E : LOGS_DIR_ERROR")
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
var logStream = fs.createWriteStream("logs/" + currentDate + ".log", {
|
||||
flags: 'a'
|
||||
});
|
||||
/**
|
||||
* Déclare une nouvelle instance de log
|
||||
* @param {string} typeName Indique le nom du type de log
|
||||
*/
|
||||
module.exports.LogType = class {
|
||||
|
||||
var sys = {
|
||||
"name":"Loguix",
|
||||
"version":"1.0.0"
|
||||
}
|
||||
constructor(typeName) {
|
||||
this.type = typeName;
|
||||
this.steps = new Map()
|
||||
this.step = this.initializeStep()
|
||||
logInstance.set(typeName, this)
|
||||
}
|
||||
|
||||
log(txt) {
|
||||
|
||||
|
||||
// [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 {
|
||||
|
||||
var logtext = "[" + this.type + "] - [INFO] - " + txt
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
|
||||
}
|
||||
|
||||
console.error("[Loguix] - Name is not configure.");
|
||||
warn(txt) {
|
||||
|
||||
var logtext = "[" + this.type + "] - [WARN] - " + txt
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
// [Function] - Fatal Error - End programm when exit
|
||||
error(txt) {
|
||||
|
||||
module.exports.stop = (endCode) => {
|
||||
|
||||
process.on('uncaughtException', () => {
|
||||
logStream.end( "["+ endCode + "]" + " - [END OF LOGS] - [" + currentDate + ']')
|
||||
console.log("check")
|
||||
logStream.close()
|
||||
});
|
||||
var logtext = "[" + this.type + "] - [ERROR] - " + txt
|
||||
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
||||
console.log(logtext)
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
nonexistentFunc();
|
||||
|
||||
|
||||
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
|
12
package.json
12
package.json
@ -1,20 +1,16 @@
|
||||
{
|
||||
"name": "loguix",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "Log System for JS",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
"debug": "node test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://gitlab.com/raphixscrap/loguix.git"
|
||||
"url": "git+https://git.raphix.fr/lib/loguix.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Raphix",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/raphixscrap/loguix/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/raphixscrap/loguix#readme"
|
||||
"license": "ISC"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user