Add abilty to overwrite style of output through constructor

This commit is contained in:
Clayton Wilson 2024-03-11 06:53:36 -05:00
parent bed65ac4bb
commit d4e976c6c4
4 changed files with 99 additions and 55 deletions

View File

@ -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
);

View File

@ -1,25 +0,0 @@
import type { DeepRequired } from "../types/generic";
import type { ConsoleStyles } from "../types/logger";
export const DEFAULT_STYLES: DeepRequired<ConsoleStyles> = {
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",
},
};

View File

@ -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<typeof ConsoleStyles>;
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<typeof StrictConsoleStyles>;

View File

@ -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", () => {