From 4742c6844cc9324d4b98a4608a5ceb9846779074 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 7 Dec 2022 01:57:21 -0500 Subject: [PATCH] *Add log level prefix plugin with chalk formatting --- package.json | 4 +++- src/config.ts | 21 +++++++++++++++++++++ yarn.lock | 12 +++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ef37687..31f34a5 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "ajv": "^8.11.0", "async-mutex": "^0.3.2", "b4a": "^1.6.1", + "chalk": "^5.1.2", "compact-encoding": "^2.11.0", "date-fns": "^2.28.0", "dotenv": "^16.0.1", @@ -45,7 +46,8 @@ "libskynet": "https://github.com/LumeWeb/libskynet.git", "libskynetnode": "https://github.com/LumeWeb/libskynetnode.git", "loady": "https://github.com/LumeWeb/loady.git", - "loglevel": "^1.8.0", + "loglevel": "^1.8.1", + "loglevel-plugin-prefix": "^0.8.4", "msgpackr": "^1.6.1", "node-cache": "^5.1.2", "node-cron": "^3.0.1", diff --git a/src/config.ts b/src/config.ts index fc5a6f1..c8f7ef4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,6 +4,8 @@ import * as os from "os"; import * as fs from "fs"; import path from "path"; import log from "loglevel"; +import chalk, { ChalkInstance } from "chalk"; +import prefix from "loglevel-plugin-prefix"; const config = new Config("lumeweb-relay"); @@ -54,4 +56,23 @@ config.load({ log.setDefaultLevel(config.get("loglevel")); +const colors = { + TRACE: chalk.magenta, + DEBUG: chalk.cyan, + INFO: chalk.blue, + WARN: chalk.yellow, + ERROR: chalk.red, +} as { [level: string]: ChalkInstance }; + +prefix.reg(log); +log.enableAll(); + +prefix.apply(log, { + format(level, name, timestamp) { + return `${chalk.gray(`[${timestamp}]`)} ${colors[level.toUpperCase()]( + level + )} ${chalk.green(`${name}:`)}`; + }, +}); + export default config; diff --git a/yarn.lock b/yarn.lock index e4d31b8..dbf0d0c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1246,6 +1246,11 @@ chalk@^4.1.2: ansi-styles "^4.1.0" supports-color "^7.1.0" +chalk@^5.1.2: + version "5.1.2" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.1.2.tgz#d957f370038b75ac572471e83be4c5ca9f8e8c45" + integrity sha512-E5CkT4jWURs1Vy5qGJye+XwCkNj7Od3Af7CP6SujMetSMkLs8Do2RWJK5yx1wamHV/op8Rz+9rltjaTQWDnEFQ== + chownr@^1.1.1: version "1.1.4" resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.4.tgz#6fc9d7b42d32a583596337666e7d08084da2cc6b" @@ -2347,7 +2352,12 @@ lodash@^4.17.20: resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== -loglevel@^1.8.0, loglevel@^1.8.1: +loglevel-plugin-prefix@^0.8.4: + version "0.8.4" + resolved "https://registry.yarnpkg.com/loglevel-plugin-prefix/-/loglevel-plugin-prefix-0.8.4.tgz#2fe0e05f1a820317d98d8c123e634c1bd84ff644" + integrity sha512-WpG9CcFAOjz/FtNht+QJeGpvVl/cdR6P0z6OcXSkr8wFJOsV2GRj2j10JLfjuA4aYkcKCNIEqRGCyTife9R8/g== + +loglevel@^1.8.1: version "1.8.1" resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.8.1.tgz#5c621f83d5b48c54ae93b6156353f555963377b4" integrity sha512-tCRIJM51SHjAayKwC+QAg8hT8vg6z7GSgLJKGvzuPb1Wc+hLzqtuVLxp6/HzSPOozuK+8ErAhy7U/sVzw8Dgfg==