From 2ec332dc6885991d2b83cba4383d0d0e4e015760 Mon Sep 17 00:00:00 2001 From: faulty Date: Mon, 2 May 2022 19:31:47 +0200 Subject: [PATCH] init --- LICENSE | 21 ++++++++++ README.md | 3 ++ index.js | 9 ++++ lib/conversion.js | 48 +++++++++++++++++++++ lib/logging.js | 104 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 27 ++++++++++++ 6 files changed, 212 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/conversion.js create mode 100644 lib/logging.js create mode 100644 package.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e1e6c58 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8decf2b --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# faulty's node js logger + +another one but now less shitty :) \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..7652a40 --- /dev/null +++ b/index.js @@ -0,0 +1,9 @@ +/* + * Faulty's Logger (FL2) + * Created for Node.JS + */ + +module.exports = { + Logger: require("./lib/logging"), + conversions: require("./lib/conversion") +}; \ No newline at end of file diff --git a/lib/conversion.js b/lib/conversion.js new file mode 100644 index 0000000..429be1f --- /dev/null +++ b/lib/conversion.js @@ -0,0 +1,48 @@ +/** properHex + * + * @param {string} val original value + * + * @returns {{hex: Array, 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 +} \ No newline at end of file diff --git a/lib/logging.js b/lib/logging.js new file mode 100644 index 0000000..ce3822d --- /dev/null +++ b/lib/logging.js @@ -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; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..b5b8d88 --- /dev/null +++ b/package.json @@ -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" +}