summaryrefslogtreecommitdiff
path: root/packages/shared/log.ts
blob: 04c516ec626b9048998be57a1099dab74d21758d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* eslint-disable no-unused-vars */

export enum LogLevel {
  Error = "ERROR",
  Warning = "WARNING",
  Info = "INFO",
  Debug = "DEBUG",
  Trace = "TRACE",
}

export const log = (message: string, level: LogLevel = LogLevel.Info) => {
  const timestamp = new Date().toISOString();
  const levelColors = {
    [LogLevel.Error]: "\x1b[31m",
    [LogLevel.Warning]: "\x1b[33m",
    [LogLevel.Info]: "\x1b[32m",
    [LogLevel.Debug]: "\x1b[34m",
    [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 = (error: unknown) => {
  log(
    `Unexpected Discord API error: ${error instanceof Error ? error.message : String(error)}`,
    LogLevel.Error,
  );
};

export const logUnexpectedDiscordAPIResult = (error: unknown) => {
  log(
    `Unexpected Discord API result: ${error instanceof Error ? error.message : String(error)}`,
    LogLevel.Error,
  );
};