This commit is contained in:
Strix 2022-05-02 19:31:47 +02:00
commit 2ec332dc68
Signed by untrusted user who does not match committer: didier
GPG key ID: 01E71F18AA4398E5
6 changed files with 212 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 faultydev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# faulty's node js logger
another one but now less shitty :)

9
index.js Normal file
View file

@ -0,0 +1,9 @@
/*
* Faulty's Logger (FL2)
* Created for Node.JS
*/
module.exports = {
Logger: require("./lib/logging"),
conversions: require("./lib/conversion")
};

48
lib/conversion.js Normal file
View file

@ -0,0 +1,48 @@
/** properHex
*
* @param {string} val original value
*
* @returns {{hex: Array<number>, str: string}}
*/
let properHex = (val) => {
let retval = {
hex: [],
str: ""
};
if (Buffer.isBuffer(val))
{
retval.str = val.toString('hex').toUpperCase().replace(/([0-9A-F]{2})/g, '$1 ');
retval.str = "[ " + retval.str.substring(0, retval.str.length - 1) + " ]";
retval.hex = val;
return retval;
}
if (typeof val === 'string')
{
let b = Buffer.from(val);
return properHex(b);
}
}
/** dictionaryConversion
*
* @param {string} origin
* @param {Map} dict
*
* @returns {string}
*
*/
let dictionaryConversion = (origin, dict) => {
let retval = "";
dict.forEach((v, k, m) => {
// if k is found in origin replace it with v
if (origin.indexOf(k) > -1) {
retval = origin.replace(k, v);
}
});
return retval;
}
module.exports = {
properHex,
dictionaryConversion
}

104
lib/logging.js Normal file
View file

@ -0,0 +1,104 @@
const conversion = require("./conversion");
class Logger {
/** Logger
*
* @param {{
* output: NodeJS.WritableStream,
* template: string,
* argsPlaceholder: string,
* historySize: number,
* defaultLevel: string
* }} options
*/
constructor(options = {})
{
this.output = options.output || process.stdout;
this.template = options.template || "[{level} {timestamp}] {message}";
this.levelsColors = { // no same colors
debug: "\x1b[32m",
info: "\x1b[36m",
warn: "\x1b[33m",
error: "\x1b[31m",
fatal: "\x1b[35m"
};
this.dict = options.dict || null;
this.argsPlaceholder = options.argsPlaceholder || "{}";
this.defaultLevel = options.defaultLevel || "info";
this.history = [];
this.historySize = options.historySize || 100;
this.onHistoryFull = options.onHistoryFull || function() {
// TODO: implement
};
}
/** log
*
* @param {string} message
* @param {any} ...args
*
*/
log (message, ...args)
{
this.alog(this.defaultLevel, message, ...args);
}
/** alog
*
* @param {string} level
* @param {string} message
* @param {any} ...args
*
*/
alog(level, message, ...args)
{
//replace placeholders with args
let msg = this.template;
msg = msg.replace(/\{timestamp\}/g, new Date().toISOString());
msg = msg.replace(/\{level\}/g, level.charAt(0).toUpperCase());
msg = msg.replace(/\{message\}/g, message);
//replace placeholders with args
let argPlaceholder = this.argsPlaceholder;
let regex = new RegExp(argPlaceholder, "g");
let stringArgCount = (regex.exec(msg) || []).length;
args.forEach((v, i, a) => {
if (Buffer.isBuffer(v))
{
v = conversion.properHex(v).str;
}
if (typeof v === 'object')
{
v = JSON.stringify(v);
}
msg = msg.replace(argPlaceholder, v);
});
if (stringArgCount < args.length)
msg += ` (${args.length - stringArgCount} args overflow.)`;
else if (stringArgCount > args.length)
msg += ` (${stringArgCount - args.length} args missing.)`;
//replace dictionary keys
if (this.dict)
msg = dictionaryConversion(msg, this.dict);
// add color
msg = this.levelsColors[level] + msg + "\x1b[0m";
//log
this.output.write(msg + "\n");
//save to history
this.history.push({
level: level,
message: message,
args: args
});
if (this.history.length >= this.historySize)
this.onHistoryFull();
}
}
module.exports = Logger;

27
package.json Normal file
View file

@ -0,0 +1,27 @@
{
"name": "logger4njs",
"version": "1.0.2",
"description": "Logger for Node JS",
"main": "index.js",
"directories": {
"lib": "lib"
},
"scripts": {
"test": "node ../test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/faultydev/logger4njs.git"
},
"keywords": [
"logger",
"log4njs",
"logging"
],
"author": "faultydev",
"license": "MIT",
"bugs": {
"url": "https://github.com/faultydev/logger4njs/issues"
},
"homepage": "https://github.com/faultydev/logger4njs#readme"
}