diff options
| author | Fuwn <[email protected]> | 2025-10-23 03:02:06 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-10-23 03:02:06 -0700 |
| commit | d8a3cc6c2d4ee0f829181c494e26f95f405d761f (patch) | |
| tree | ec4e3e01e87244fb775db0bba1963a48196d55c1 | |
| parent | feat(gateway): Migrate remaining console logs to shared logging framework (diff) | |
| download | umabotdiscord-d8a3cc6c2d4ee0f829181c494e26f95f405d761f.tar.xz umabotdiscord-d8a3cc6c2d4ee0f829181c494e26f95f405d761f.zip | |
feat(shared:log): Improve unit logging
| -rw-r--r-- | packages/shared/log.ts | 67 |
1 files changed, 42 insertions, 25 deletions
diff --git a/packages/shared/log.ts b/packages/shared/log.ts index 92ab735..46bcd7b 100644 --- a/packages/shared/log.ts +++ b/packages/shared/log.ts @@ -8,7 +8,35 @@ export enum LogLevel { Trace = "TRACE", } -export const log = (message: string, level: LogLevel = LogLevel.Info) => { +const extractUnitFromStack = (stackOffset: number = 2): string | undefined => { + const callingLine = new Error().stack?.split("\n")[stackOffset]?.trim(); + return ( + callingLine + ?.match(/packages\/(.*?):\d+/)?.[1] + ?.replace(/\//g, "::") + ?.replace(/::src::/, "::") + ?.replace(/\.ts$/, "") + + "::" + + callingLine?.match(/:(\d+):/)?.[1] + ); +}; + +const createLogWrapper = ( + messageTemplate: (error: unknown) => string, + level: LogLevel, + stackOffset: number = 2, +) => { + return (error: unknown) => { + const unit = extractUnitFromStack(stackOffset); + log(messageTemplate(error), level, unit); + }; +}; + +export const log = ( + message: string, + level: LogLevel = LogLevel.Info, + customUnit?: string, +) => { const timestamp = new Date().toISOString(); const levelColors = { [LogLevel.Error]: "\x1b[31m", @@ -18,38 +46,27 @@ export const log = (message: string, level: LogLevel = LogLevel.Info) => { [LogLevel.Trace]: "\x1b[35m", }; const levelColor = levelColors[level]; - const callingLine = new Error().stack?.split("\n")[2].trim(); - const unit = - callingLine - ?.match(/packages\/(.*?):\d+/)?.[1] - ?.replace(/\//g, "::") - ?.replace(/::src::/, "::") - ?.replace(/\.ts$/, "") + - "::" + - callingLine?.match(/:(\d+):/)?.[1]; + const unit = customUnit ?? extractUnitFromStack(2); console.log( `\x1b[37m${timestamp}\x1b[0m ${levelColor}${level}\x1b[0m \x1b[1m${unit}\x1b[22m \x1b[37m> ${message}\x1b[0m`, ); }; -export const logUnexpectedDiscordAPIError = (error: unknown) => { - log( +export const logUnexpectedDiscordAPIError = createLogWrapper( + (error: unknown) => `Unexpected Discord API error: ${error instanceof Error ? error.message : String(error)}`, - LogLevel.Error, - ); -}; + LogLevel.Error, +); -export const logUnexpectedDiscordAPIResult = (error: unknown) => { - log( +export const logUnexpectedDiscordAPIResult = createLogWrapper( + (error: unknown) => `Unexpected Discord API result: ${error instanceof Error ? error.message : String(error)}`, - LogLevel.Error, - ); -}; + LogLevel.Error, +); -export const logUnexpectedAIError = (error: unknown) => { - log( +export const logUnexpectedAIError = createLogWrapper( + (error: unknown) => `Unexpected AI error: ${error instanceof Error ? error.message : String(error)}`, - LogLevel.Error, - ); -}; + LogLevel.Error, +); |