This commit is contained in:
parent
be8073ea02
commit
cb4a05ea25
5 changed files with 143 additions and 32 deletions
45
src/index.ts
45
src/index.ts
|
@ -7,8 +7,10 @@ import {
|
|||
statSync,
|
||||
} from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { getCallerInfo, parsePattern } from "@lib/char";
|
||||
import { format, inspect } from "node:util";
|
||||
import { getCallerInfo, getTimestamp, parsePattern } from "@lib/char";
|
||||
import {
|
||||
ansiColors,
|
||||
defaultConfig,
|
||||
loadEnvConfig,
|
||||
loadLoggerConfig,
|
||||
|
@ -111,6 +113,47 @@ class Echo {
|
|||
public trace(data: unknown): void {
|
||||
this.log("trace", data);
|
||||
}
|
||||
|
||||
public custom(tag: string, context: string, message: unknown): void {
|
||||
if (this.config.silent || !this.config.console) return;
|
||||
|
||||
const timestamps = getTimestamp(this.config);
|
||||
|
||||
const normalizedTag = tag.toUpperCase();
|
||||
const tagColor = this.config.consoleColor
|
||||
? (ansiColors[this.config.customColors?.[normalizedTag] ?? "green"] ?? "")
|
||||
: "";
|
||||
const contextColor = this.config.consoleColor ? ansiColors.cyan : "";
|
||||
const gray = this.config.consoleColor ? ansiColors.gray : "";
|
||||
const reset = this.config.consoleColor ? ansiColors.reset : "";
|
||||
|
||||
const resolvedData =
|
||||
this.config.prettyPrint && typeof message === "object" && message !== null
|
||||
? inspect(message, {
|
||||
depth: null,
|
||||
colors: this.config.consoleColor,
|
||||
breakLength: 1,
|
||||
compact: false,
|
||||
})
|
||||
: format(message);
|
||||
|
||||
const pattern =
|
||||
this.config.customPattern ??
|
||||
"{color:gray}{pretty-timestamp}{reset} {color:tagColor}[{tag}]{reset} {color:contextColor}({context}){reset} {data}";
|
||||
|
||||
const line = pattern
|
||||
.replace(/{timestamp}/g, timestamps.timestamp)
|
||||
.replace(/{pretty-timestamp}/g, timestamps.prettyTimestamp)
|
||||
.replace(/{tag}/g, tag)
|
||||
.replace(/{context}/g, context)
|
||||
.replace(/{data}/g, resolvedData)
|
||||
.replace(/{color:gray}/g, gray)
|
||||
.replace(/{color:tagColor}/g, tagColor)
|
||||
.replace(/{color:contextColor}/g, contextColor)
|
||||
.replace(/{reset}/g, reset);
|
||||
|
||||
console.log(line);
|
||||
}
|
||||
}
|
||||
|
||||
const echo = new Echo();
|
||||
|
|
|
@ -146,7 +146,7 @@ function parsePattern(ctx: PatternContext): string {
|
|||
config.prettyPrint && typeof data === "object" && data !== null
|
||||
? inspect(data, {
|
||||
depth: null,
|
||||
colors: true,
|
||||
colors: config.consoleColor,
|
||||
breakLength: 1,
|
||||
compact: false,
|
||||
})
|
||||
|
@ -155,7 +155,8 @@ function parsePattern(ctx: PatternContext): string {
|
|||
const numericLevel: LogLevelValue = logLevelValues[level];
|
||||
|
||||
const final = config.pattern
|
||||
.replace(/{timestamp}/g, config.prettyPrint ? prettyTimestamp : timestamp)
|
||||
.replace(/{timestamp}/g, timestamp)
|
||||
.replace(/{pretty-timestamp}/g, prettyTimestamp)
|
||||
.replace(/{level-name}/g, level.toUpperCase())
|
||||
.replace(/{level}/g, String(numericLevel))
|
||||
.replace(/{file-name}/g, fileName)
|
||||
|
@ -167,4 +168,4 @@ function parsePattern(ctx: PatternContext): string {
|
|||
return replaceColorTokens(final, level, config);
|
||||
}
|
||||
|
||||
export { parsePattern, getCallerInfo };
|
||||
export { parsePattern, getCallerInfo, getTimestamp, generateShortId };
|
||||
|
|
|
@ -54,7 +54,7 @@ const defaultConfig: Required<LoggerConfig> = {
|
|||
silent: false,
|
||||
|
||||
pattern:
|
||||
"{color:gray}{timestamp}{reset} {color:levelColor}[{level-name}]{reset} {color:gray}({reset}{file-name}:{line}:{column}{color:gray}){reset} {data}",
|
||||
"{color:gray}{pretty-timestamp}{reset} {color:levelColor}[{level-name}]{reset} {color:gray}({reset}{file-name}:{line}:{column}{color:gray}){reset} {data}",
|
||||
|
||||
levelColor: {
|
||||
trace: "cyan",
|
||||
|
@ -65,6 +65,10 @@ const defaultConfig: Required<LoggerConfig> = {
|
|||
fatal: "red",
|
||||
},
|
||||
|
||||
customColors: {},
|
||||
customPattern:
|
||||
"{color:gray}{pretty-timestamp}{reset} {color:tagColor}[{tag}]{reset} {color:contextColor}({context}){reset} {data}",
|
||||
|
||||
prettyPrint: true,
|
||||
};
|
||||
|
||||
|
@ -100,6 +104,30 @@ function loadEnvConfig(): LoggerConfig {
|
|||
if (process.env.LOG_PRETTY_PRINT)
|
||||
config.prettyPrint = process.env.LOG_PRETTY_PRINT === "true";
|
||||
|
||||
if (process.env.LOG_LEVEL_COLOR) {
|
||||
const colors = process.env.LOG_LEVEL_COLOR.split(",");
|
||||
for (const color of colors) {
|
||||
const [level, colorName] = color.split(":");
|
||||
if (logLevelValues[level as LogLevel] !== undefined) {
|
||||
config.levelColor = {
|
||||
...config.levelColor,
|
||||
[level as LogLevel]: colorName as keyof typeof ansiColors,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (process.env.LOG_CUSTOM_COLORS) {
|
||||
const colors = process.env.LOG_CUSTOM_COLORS.split(",");
|
||||
for (const color of colors) {
|
||||
const [tag, colorName] = color.split(":");
|
||||
config.customColors = {
|
||||
...config.customColors,
|
||||
[tag]: colorName as keyof typeof ansiColors,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue