feat: new logger yo
This commit is contained in:
+44
-24
@@ -1,5 +1,6 @@
|
|||||||
import { feature } from "bun:bundle";
|
import { feature } from "bun:bundle";
|
||||||
import { InspectColor, styleText } from "node:util";
|
import { InspectColor, styleText } from "node:util";
|
||||||
|
|
||||||
import { inspect } from "bun";
|
import { inspect } from "bun";
|
||||||
|
|
||||||
const colors: InspectColor[] = [
|
const colors: InspectColor[] = [
|
||||||
@@ -20,36 +21,55 @@ const colors: InspectColor[] = [
|
|||||||
];
|
];
|
||||||
var i = 0;
|
var i = 0;
|
||||||
|
|
||||||
if (!feature("API"))
|
if (!feature("prod"))
|
||||||
console.log(
|
console.log(
|
||||||
colors.map((c, i) => styleText(c, (i + 10).toString(36))).join(""),
|
colors.map((c, i) => styleText(c, (i + 10).toString(36))).join(""),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export type Logger = {
|
||||||
|
(strings: TemplateStringsArray, ...values: unknown[]): void;
|
||||||
|
error(strings: TemplateStringsArray, ...values: unknown[]): void;
|
||||||
|
warn(strings: TemplateStringsArray, ...values: unknown[]): void;
|
||||||
|
debug(strings: TemplateStringsArray, ...values: unknown[]): void;
|
||||||
|
};
|
||||||
|
|
||||||
export function createLogger(label?: string) {
|
export function createLogger(label?: string) {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const col = colors[i];
|
const col = colors[i];
|
||||||
i = (i + 1) % colors.length;
|
i = (i + 1) % colors.length;
|
||||||
return function log(strings: TemplateStringsArray, ...values: unknown[]) {
|
function mkLogFn(stream: Console | NodeJS.WriteStream, strip?: InspectColor) {
|
||||||
const normalized = styleText(
|
return (strings: TemplateStringsArray, ...values: unknown[]) => {
|
||||||
col,
|
const normalized = styleText(
|
||||||
`${(label ?? `${Date.now() - start}ms`).padStart(7)} | ${strings
|
col,
|
||||||
.reduce(
|
`${(strip ? styleText : (_: string, s: string) => s)("bold", (label ?? `${Date.now() - start}ms`).padStart(7))} ▌${strip ? styleText(strip, "▌") : " "}${strings
|
||||||
(out, str, i) =>
|
.reduce(
|
||||||
out +
|
(out, str, i) =>
|
||||||
str +
|
out +
|
||||||
(typeof values[i] === "string"
|
styleText(col, str) +
|
||||||
? values[i]
|
(typeof values[i] === "string"
|
||||||
: typeof values[i] !== "undefined"
|
? values[i]
|
||||||
? inspect(values[i], { colors: true })
|
: typeof values[i] !== "undefined"
|
||||||
: ""),
|
? inspect(values[i], { colors: true })
|
||||||
"",
|
: ""),
|
||||||
)
|
"",
|
||||||
.split("\n")
|
)
|
||||||
.map((l, i) => styleText(col, i ? `${" ".repeat(7)} | ${l}` : l))
|
.split("\n")
|
||||||
.join("\n")}\n`,
|
.map((l, i) =>
|
||||||
);
|
styleText(
|
||||||
console.write(normalized);
|
col,
|
||||||
};
|
i
|
||||||
|
? `${" ".repeat(7)} ▌${strip ? styleText(strip, "▌") : " "}${l}`
|
||||||
|
: l,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.join("\n")}\n`,
|
||||||
|
);
|
||||||
|
(stream.write as (data: string) => void)(normalized);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const log = mkLogFn(console) as Logger;
|
||||||
|
log.error = mkLogFn(process.stderr, "redBright");
|
||||||
|
log.warn = mkLogFn(console, "yellow");
|
||||||
|
log.debug = feature("prod") ? () => {} : mkLogFn(console, "blue");
|
||||||
|
return log;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Logger = ReturnType<typeof createLogger>;
|
|
||||||
|
|||||||
Reference in New Issue
Block a user