-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatMessageDefault.ts
62 lines (59 loc) · 2.1 KB
/
formatMessageDefault.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Who } from "who-am-i-now";
import {
bgDarkGray,
bgLightGray,
bgMagenta,
bgRed,
bgYellow,
black,
white,
} from "ansicolor";
import { LogLevel } from "../types/LogLevel";
import { CHEESE_ICON } from "./cheeseIcon";
import { getStackTrace } from "./getStackTrace";
import { FormatMessageFn } from "../types/FormatMessageFn";
import { getTimestamp } from "../helpers/getTimestamp";
export const formatMessageDefault: FormatMessageFn = (
message: string,
who: Who,
showLogLevel: boolean,
logLevel: LogLevel,
millisecondsSince1970: number,
showDate: boolean,
dateFormat: string,
showOrigin: boolean,
autoColorizeObject: boolean,
showCheeseIcon: boolean,
allColorsDisabled: boolean,
colorOverride: string,
messagePrefix: string,
messageSuffix: string,
) => {
const cheeseIconPrepared = showCheeseIcon ? CHEESE_ICON + " " : "";
const messagePrefixPrepared = messagePrefix ? messagePrefix + " " : "";
const messageSuffixPrepared = messageSuffix ?? "";
const logLevelPrepared = showLogLevel
? "[" + logLevel + "]" + (showDate ? " " : "")
: "";
const datePrepared = showDate
? getTimestamp(dateFormat, millisecondsSince1970)
: "";
const originInfo = showOrigin ? getStackTrace() ?? "" : "";
let prefixPrepared = `${cheeseIconPrepared}${messagePrefixPrepared}${logLevelPrepared}${datePrepared}`;
if (!allColorsDisabled) {
const textColorFn = who.isServerApp ? black : white;
if (logLevel === LogLevel.error) {
prefixPrepared = bgRed(textColorFn(prefixPrepared));
} else if (logLevel === LogLevel.warn) {
prefixPrepared = bgYellow(textColorFn(prefixPrepared));
} else if (logLevel === LogLevel.info) {
prefixPrepared = bgLightGray(textColorFn(prefixPrepared));
} else if (logLevel === LogLevel.log) {
prefixPrepared = bgDarkGray(textColorFn(prefixPrepared));
} else if (logLevel === LogLevel.debug) {
prefixPrepared = bgMagenta(textColorFn(prefixPrepared));
}
}
const originInfoPrepared = originInfo ? ` ${originInfo}\n` : "";
return `${prefixPrepared}\n${originInfoPrepared}${message}${messageSuffixPrepared}`;
};