|
1 |
| -import { isBoolean, noop } from 'lodash'; |
| 1 | +import log from 'loglevel'; |
2 | 2 |
|
3 |
| -const SHOULD_LOG = process.env.NODE_ENV !== 'production'; |
| 3 | +const SHOULD_LOG = process.env.SHOULD_LOG === 'true'; |
4 | 4 |
|
5 |
| -type Console = Pick<typeof console, 'info' | 'error' | 'debug' | 'warn' | 'log'>; |
| 5 | +const FnLoggerService = log.getLogger('[FN Grafana]'); |
6 | 6 |
|
7 |
| -export class FnLoggerService { |
8 |
| - private static readonly DEFAULT_SHOULD_LOG = false; |
| 7 | +FnLoggerService.setLevel(SHOULD_LOG ? log.levels.DEBUG : log.levels.ERROR); |
9 | 8 |
|
10 |
| - private static logger(shouldLog: boolean | null) { |
11 |
| - /* eslint-disable-next-line */ |
12 |
| - const flag = isBoolean(shouldLog) |
13 |
| - ? shouldLog |
14 |
| - : isBoolean(SHOULD_LOG) |
15 |
| - ? SHOULD_LOG |
16 |
| - : FnLoggerService.DEFAULT_SHOULD_LOG; |
17 |
| - |
18 |
| - if (flag) { |
19 |
| - return console as Console; |
20 |
| - } |
21 |
| - |
22 |
| - const noopConsole: Console = { |
23 |
| - info: noop, |
24 |
| - error: noop, |
25 |
| - debug: noop, |
26 |
| - warn: noop, |
27 |
| - log: noop, |
28 |
| - }; |
29 |
| - |
30 |
| - return noopConsole; |
31 |
| - } |
32 |
| - |
33 |
| - /* eslint-disable @typescript-eslint/no-explicit-any */ |
34 |
| - static debug = (shouldLog: boolean | null, ...args: any[]) => { |
35 |
| - FnLoggerService.logger(shouldLog).debug(FnLoggerService.valuesToString(...args)); |
36 |
| - }; |
37 |
| - |
38 |
| - static error = (shouldLog: boolean | null, ...args: any[]) => { |
39 |
| - FnLoggerService.logger(shouldLog).error(FnLoggerService.valuesToString(...args)); |
40 |
| - }; |
41 |
| - |
42 |
| - static warn = (shouldLog: boolean | null, ...args: any[]) => { |
43 |
| - FnLoggerService.logger(shouldLog).warn(FnLoggerService.valuesToString(...args)); |
44 |
| - }; |
45 |
| - |
46 |
| - static info = (shouldLog: boolean | null, ...args: any[]) => { |
47 |
| - FnLoggerService.logger(shouldLog).info(FnLoggerService.valuesToString(...args)); |
48 |
| - }; |
49 |
| - |
50 |
| - static log = (shouldLog: boolean | null, ...args: any[]) => { |
51 |
| - FnLoggerService.logger(shouldLog).log(FnLoggerService.valuesToString(...args)); |
52 |
| - }; |
53 |
| - /* eslint-enable @typescript-eslint/no-explicit-any */ |
54 |
| - |
55 |
| - /* eslint-disable @typescript-eslint/no-explicit-any */ |
56 |
| - private static readonly valuesToString = (...args: any[]): string => |
57 |
| - args.map(FnLoggerService.valueToString).join(' '); |
58 |
| - /* eslint-enable @typescript-eslint/no-explicit-any */ |
59 |
| - |
60 |
| - private static readonly valueToString = <V>(value: V): string => { |
61 |
| - if (typeof value === 'string') { |
62 |
| - return value; |
63 |
| - } |
64 |
| - |
65 |
| - if (Array.isArray(value)) { |
66 |
| - return value.map(FnLoggerService.valueToString).join(' '); |
67 |
| - } |
68 |
| - |
69 |
| - if (value instanceof Error) { |
70 |
| - return value.message; |
71 |
| - } |
72 |
| - |
73 |
| - try { |
74 |
| - return JSON.stringify(value); |
75 |
| - } catch { |
76 |
| - return String(value); |
77 |
| - } |
78 |
| - }; |
79 |
| -} |
| 9 | +export { FnLoggerService }; |
0 commit comments