From d4e976c6c465f62a052d1922fea7756fc7f36e20 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Mon, 11 Mar 2024 06:53:36 -0500 Subject: [PATCH] Add abilty to overwrite style of output through constructor --- src/Logger.ts | 15 +++---- src/lib/defaults.ts | 25 ----------- src/types/logger.ts | 102 +++++++++++++++++++++++++++++++++---------- tests/logger.spec.ts | 12 +++++ 4 files changed, 99 insertions(+), 55 deletions(-) delete mode 100644 src/lib/defaults.ts diff --git a/src/Logger.ts b/src/Logger.ts index be44295..03386b5 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -5,7 +5,6 @@ import { StrictLogOutputs, } from "./types/logger"; import { gzip, randomString, stringifyInstance, ungzip } from "./lib/utilities"; -import { DEFAULT_STYLES } from "./lib/defaults"; import { LogContext, BucketInfo, LogMeta, LogConfig } from "./types/logger"; @@ -112,13 +111,13 @@ export class Logger { } private consolePrint(label: LogLabel, message: string, meta: LogMeta) { - const style = `background: ${DEFAULT_STYLES[label].background}; color: ${DEFAULT_STYLES[label].color}; font-weight: bold; border-radius: 4px;`; + const styleFormatter = `background: ${this.outputs.console.style[label].backgroundColor}; color: ${this.outputs.console.style[label].textColor}; font-weight: bold; border-radius: 4px;`; switch (label) { case LogLabel.TRACE: console.trace( `%c ${label} ` + `%c ${message}`, - style, + styleFormatter, MESSAGE_STYLE, meta ); @@ -126,7 +125,7 @@ export class Logger { case LogLabel.DEBUG: console.debug( `%c ${label} ` + `%c ${message}`, - style, + styleFormatter, MESSAGE_STYLE, meta ); @@ -134,7 +133,7 @@ export class Logger { case LogLabel.INFO: console.info( `%c ${label} ` + `%c ${message}`, - style, + styleFormatter, MESSAGE_STYLE, meta ); @@ -142,7 +141,7 @@ export class Logger { case LogLabel.WARN: console.warn( `%c ${label} ` + `%c ${message}`, - style, + styleFormatter, MESSAGE_STYLE, meta ); @@ -150,7 +149,7 @@ export class Logger { case LogLabel.FATAL: console.error( `%c ${label} ` + `%c ${message}`, - style, + styleFormatter, MESSAGE_STYLE, meta ); @@ -158,7 +157,7 @@ export class Logger { default: console.log( `%c ${label} ` + `%c ${message}`, - style, + styleFormatter, MESSAGE_STYLE, meta ); diff --git a/src/lib/defaults.ts b/src/lib/defaults.ts deleted file mode 100644 index a25e37e..0000000 --- a/src/lib/defaults.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { DeepRequired } from "../types/generic"; -import type { ConsoleStyles } from "../types/logger"; - -export const DEFAULT_STYLES: DeepRequired = { - trace: { - background: "#949494", - color: "#fff", - }, - debug: { - background: "#fe7bf3", - color: "#fff", - }, - info: { - background: "#65f10e", - color: "#fff", - }, - warn: { - background: "#faf200", - color: "#000", - }, - fatal: { - background: "#cc0018", - color: "#fff", - }, -}; diff --git a/src/types/logger.ts b/src/types/logger.ts index b6ef956..a357ce5 100644 --- a/src/types/logger.ts +++ b/src/types/logger.ts @@ -26,10 +26,88 @@ export interface ConsoleOutputOpts { enabled: boolean; } +export const ConsoleStyles = z + .object({ + trace: z + .object({ + backgroundColor: z.string().default("#949494").optional(), + textColor: z.string().default("#fff").optional(), + }) + .default({}) + .optional(), + debug: z + .object({ + backgroundColor: z.string().default("#fe7bf3").optional(), + textColor: z.string().default("#fff").optional(), + }) + .default({}) + .optional(), + info: z + .object({ + backgroundColor: z.string().default("#65f10e").optional(), + textColor: z.string().default("#fff").optional(), + }) + .default({}) + .optional(), + warn: z + .object({ + backgroundColor: z.string().default("#faf200").optional(), + textColor: z.string().default("#000").optional(), + }) + .default({}) + .optional(), + fatal: z + .object({ + backgroundColor: z.string().default("#cc0018").optional(), + textColor: z.string().default("#fff").optional(), + }) + .default({}) + .optional(), + }) + .optional(); + +export type ConsoleStyles = z.infer; + +export const StrictConsoleStyles = z + .object({ + trace: z + .object({ + backgroundColor: z.string().default("#949494"), + textColor: z.string().default("#fff"), + }) + .default({}), + debug: z + .object({ + backgroundColor: z.string().default("#fe7bf3"), + textColor: z.string().default("#fff"), + }) + .default({}), + info: z + .object({ + backgroundColor: z.string().default("#65f10e"), + textColor: z.string().default("#fff"), + }) + .default({}), + warn: z + .object({ + backgroundColor: z.string().default("#faf200"), + textColor: z.string().default("#000"), + }) + .default({}), + fatal: z + .object({ + backgroundColor: z.string().default("#cc0018"), + textColor: z.string().default("#fff"), + }) + .default({}), + }) + .default({}); + export const LogOutputs = z.object({ console: z .object({ enabled: z.boolean().default(true).optional(), + style: ConsoleStyles, }) .default({}) .optional(), @@ -57,6 +135,7 @@ export const StrictLogOutputs = z.object({ console: z .object({ enabled: z.boolean().default(true), + style: StrictConsoleStyles, }) .default({}), tampermonkey: z @@ -94,25 +173,4 @@ export interface BucketInfo { createdAt: number; } -export interface ConsoleStyles { - trace?: { - background?: string; - color?: string; - }; - debug?: { - background?: string; - color?: string; - }; - info?: { - background?: string; - color?: string; - }; - warn?: { - background?: string; - color?: string; - }; - fatal?: { - background?: string; - color?: string; - }; -} +export type StrictConsoleStyles = z.infer; diff --git a/tests/logger.spec.ts b/tests/logger.spec.ts index 08e1d8e..4ee12e2 100644 --- a/tests/logger.spec.ts +++ b/tests/logger.spec.ts @@ -34,6 +34,18 @@ test("Constructors", () => { callback: () => {}, }, }); + const _logger8 = new Logger({ + outputs: { + console: { + style: { + trace: { backgroundColor: "#ababab", textColor: "#bababa" }, + debug: { backgroundColor: "#436ba3", textColor: "#197921" }, + info: { backgroundColor: "#9991aa", textColor: "#906851" }, + warn: { backgroundColor: "#deadbe", textColor: "#ad7ce3" }, + }, + }, + }, + }); }); test("Log Messages", () => {