summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-10-20 22:20:56 -0700
committerFuwn <[email protected]>2025-10-20 22:20:56 -0700
commite9f35567fe05c1ffb6935c8e3f95290f3687c236 (patch)
treeb2bc67ac30842b01d87f57abfb60574d073aa991
parentfeat(shared:log): Add standard Discord API error log (diff)
downloadumabotdiscord-e9f35567fe05c1ffb6935c8e3f95290f3687c236.tar.xz
umabotdiscord-e9f35567fe05c1ffb6935c8e3f95290f3687c236.zip
feat(shared:log): Improve unit logging
-rw-r--r--packages/shared/log.ts18
1 files changed, 11 insertions, 7 deletions
diff --git a/packages/shared/log.ts b/packages/shared/log.ts
index ce50dd5..965f7f5 100644
--- a/packages/shared/log.ts
+++ b/packages/shared/log.ts
@@ -8,11 +8,7 @@ export enum LogLevel {
Trace = "TRACE",
}
-export const log = (
- unit: string,
- message: string,
- level: LogLevel = LogLevel.Info,
-) => {
+export const log = (message: string, level: LogLevel = LogLevel.Info) => {
const timestamp = new Date().toISOString();
const levelColors = {
[LogLevel.Error]: "\x1b[31m",
@@ -22,15 +18,23 @@ export const log = (
[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];
console.log(
`\x1b[37m${timestamp}\x1b[0m ${levelColor}${level}\x1b[0m \x1b[1m${unit}\x1b[22m \x1b[37m> ${message}\x1b[0m`,
);
};
-export const logUnexpectedDiscordAPIError = (unit: string, error: unknown) => {
+export const logUnexpectedDiscordAPIError = (error: unknown) => {
log(
- unit,
`Unexpected Discord API error: ${error instanceof Error ? error.message : String(error)}`,
LogLevel.Error,
);