mirror of
https://github.com/ClaytonWWilson/stokpile.git
synced 2025-12-16 08:58:47 +00:00
Add abilty to overwrite style of output through constructor
This commit is contained in:
parent
bed65ac4bb
commit
d4e976c6c4
@ -5,7 +5,6 @@ import {
|
|||||||
StrictLogOutputs,
|
StrictLogOutputs,
|
||||||
} from "./types/logger";
|
} from "./types/logger";
|
||||||
import { gzip, randomString, stringifyInstance, ungzip } from "./lib/utilities";
|
import { gzip, randomString, stringifyInstance, ungzip } from "./lib/utilities";
|
||||||
import { DEFAULT_STYLES } from "./lib/defaults";
|
|
||||||
|
|
||||||
import { LogContext, BucketInfo, LogMeta, LogConfig } from "./types/logger";
|
import { LogContext, BucketInfo, LogMeta, LogConfig } from "./types/logger";
|
||||||
|
|
||||||
@ -112,13 +111,13 @@ export class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private consolePrint(label: LogLabel, message: string, meta: LogMeta) {
|
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) {
|
switch (label) {
|
||||||
case LogLabel.TRACE:
|
case LogLabel.TRACE:
|
||||||
console.trace(
|
console.trace(
|
||||||
`%c ${label} ` + `%c ${message}`,
|
`%c ${label} ` + `%c ${message}`,
|
||||||
style,
|
styleFormatter,
|
||||||
MESSAGE_STYLE,
|
MESSAGE_STYLE,
|
||||||
meta
|
meta
|
||||||
);
|
);
|
||||||
@ -126,7 +125,7 @@ export class Logger {
|
|||||||
case LogLabel.DEBUG:
|
case LogLabel.DEBUG:
|
||||||
console.debug(
|
console.debug(
|
||||||
`%c ${label} ` + `%c ${message}`,
|
`%c ${label} ` + `%c ${message}`,
|
||||||
style,
|
styleFormatter,
|
||||||
MESSAGE_STYLE,
|
MESSAGE_STYLE,
|
||||||
meta
|
meta
|
||||||
);
|
);
|
||||||
@ -134,7 +133,7 @@ export class Logger {
|
|||||||
case LogLabel.INFO:
|
case LogLabel.INFO:
|
||||||
console.info(
|
console.info(
|
||||||
`%c ${label} ` + `%c ${message}`,
|
`%c ${label} ` + `%c ${message}`,
|
||||||
style,
|
styleFormatter,
|
||||||
MESSAGE_STYLE,
|
MESSAGE_STYLE,
|
||||||
meta
|
meta
|
||||||
);
|
);
|
||||||
@ -142,7 +141,7 @@ export class Logger {
|
|||||||
case LogLabel.WARN:
|
case LogLabel.WARN:
|
||||||
console.warn(
|
console.warn(
|
||||||
`%c ${label} ` + `%c ${message}`,
|
`%c ${label} ` + `%c ${message}`,
|
||||||
style,
|
styleFormatter,
|
||||||
MESSAGE_STYLE,
|
MESSAGE_STYLE,
|
||||||
meta
|
meta
|
||||||
);
|
);
|
||||||
@ -150,7 +149,7 @@ export class Logger {
|
|||||||
case LogLabel.FATAL:
|
case LogLabel.FATAL:
|
||||||
console.error(
|
console.error(
|
||||||
`%c ${label} ` + `%c ${message}`,
|
`%c ${label} ` + `%c ${message}`,
|
||||||
style,
|
styleFormatter,
|
||||||
MESSAGE_STYLE,
|
MESSAGE_STYLE,
|
||||||
meta
|
meta
|
||||||
);
|
);
|
||||||
@ -158,7 +157,7 @@ export class Logger {
|
|||||||
default:
|
default:
|
||||||
console.log(
|
console.log(
|
||||||
`%c ${label} ` + `%c ${message}`,
|
`%c ${label} ` + `%c ${message}`,
|
||||||
style,
|
styleFormatter,
|
||||||
MESSAGE_STYLE,
|
MESSAGE_STYLE,
|
||||||
meta
|
meta
|
||||||
);
|
);
|
||||||
|
|||||||
@ -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",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@ -26,10 +26,88 @@ export interface ConsoleOutputOpts {
|
|||||||
enabled: boolean;
|
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({
|
export const LogOutputs = z.object({
|
||||||
console: z
|
console: z
|
||||||
.object({
|
.object({
|
||||||
enabled: z.boolean().default(true).optional(),
|
enabled: z.boolean().default(true).optional(),
|
||||||
|
style: ConsoleStyles,
|
||||||
})
|
})
|
||||||
.default({})
|
.default({})
|
||||||
.optional(),
|
.optional(),
|
||||||
@ -57,6 +135,7 @@ export const StrictLogOutputs = z.object({
|
|||||||
console: z
|
console: z
|
||||||
.object({
|
.object({
|
||||||
enabled: z.boolean().default(true),
|
enabled: z.boolean().default(true),
|
||||||
|
style: StrictConsoleStyles,
|
||||||
})
|
})
|
||||||
.default({}),
|
.default({}),
|
||||||
tampermonkey: z
|
tampermonkey: z
|
||||||
@ -94,25 +173,4 @@ export interface BucketInfo {
|
|||||||
createdAt: number;
|
createdAt: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConsoleStyles {
|
export type StrictConsoleStyles = z.infer<typeof StrictConsoleStyles>;
|
||||||
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;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
@ -34,6 +34,18 @@ test("Constructors", () => {
|
|||||||
callback: () => {},
|
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", () => {
|
test("Log Messages", () => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user