summaryrefslogtreecommitdiff
path: root/packages/shared/log.ts
blob: 35a3590a64000dc425638d2cfec792d09c3263a8 (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
/* eslint-disable no-unused-vars */

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

export const log = (
  unit: string,
  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];

  console.log(
    `\x1b[37m${timestamp}\x1b[0m ${levelColor}${level}\x1b[0m \x1b[1m${unit}\x1b[22m \x1b[37m> ${message}\x1b[0m`,
  );
};