|
1 | | -import log from 'loglevel'; |
| 1 | +import log from "loglevel"; |
2 | 2 |
|
3 | | -log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'debug'); |
| 3 | +log.setLevel(process.env.NODE_ENV === "production" ? "debug" : "debug"); |
4 | 4 |
|
5 | 5 | function getCallerInfo() { |
6 | | - const stack = new Error().stack; |
7 | | - if (!stack) { |
8 | | - return ''; |
9 | | - } |
| 6 | + const stack = new Error().stack; |
| 7 | + if (!stack) { |
| 8 | + return ""; |
| 9 | + } |
10 | 10 |
|
11 | | - const stackLines = stack.split('\n'); |
12 | | - const callerLine = stackLines[3] || ''; |
13 | | - const matchResult = callerLine.match(/\((.*):(\d+):(\d+)\)$/); |
| 11 | + const stackLines = stack.split("\n"); |
| 12 | + const callerLine = stackLines[3] || ""; |
| 13 | + const matchResult = callerLine.match(/\((.*):(\d+):(\d+)\)$/); |
14 | 14 |
|
15 | | - if (matchResult && matchResult.length === 4) { |
16 | | - const file = matchResult[1]; |
17 | | - const line = matchResult[2]; |
18 | | - const column = matchResult[3]; |
19 | | - return `${file}:${line}:${column}`; |
20 | | - } |
21 | | - return ''; |
| 15 | + if (matchResult && matchResult.length === 4) { |
| 16 | + const file = matchResult[1]; |
| 17 | + const line = matchResult[2]; |
| 18 | + const column = matchResult[3]; |
| 19 | + return `${file}:${line}:${column}`; |
| 20 | + } |
| 21 | + return ""; |
22 | 22 | } |
23 | 23 |
|
24 | 24 | const createLoggerFunction = |
25 | | - (logFunction: (...args: any[]) => void) => |
26 | | - (message: string, ...args: any[]) => { |
27 | | - const callerInfo = getCallerInfo(); |
28 | | - logFunction(`${message} (${callerInfo})`, ...args); |
29 | | - }; |
| 25 | + (logFunction: (...args: any[]) => void) => |
| 26 | + (message: string, ...args: any[]) => { |
| 27 | + const callerInfo = getCallerInfo(); |
| 28 | + logFunction(`${message} (${callerInfo})`, ...args); |
| 29 | + }; |
30 | 30 |
|
31 | 31 | // Function to start a log group |
32 | 32 | const startGroup = (groupName: string) => { |
33 | | - const callerInfo = getCallerInfo(); |
34 | | - console.group(`${groupName} (${callerInfo})`); |
| 33 | + const callerInfo = getCallerInfo(); |
| 34 | + console.group(`${groupName} (${callerInfo})`); |
35 | 35 | }; |
36 | 36 |
|
37 | 37 | // Function to end a log group |
38 | 38 | const endGroup = () => { |
39 | | - console.groupEnd(); |
| 39 | + console.groupEnd(); |
40 | 40 | }; |
41 | 41 |
|
42 | 42 | // Helper function to log multiple messages within a group |
43 | | -const logGroup = (groupName: string, messages: { level: keyof typeof log; message: string; args?: any[] }[]) => { |
44 | | - startGroup(groupName); |
45 | | - messages.forEach(({ level, message, args }) => { |
46 | | - if (typeof log[level] === 'function') { |
47 | | - createLoggerFunction(log[level] as (...args: any[]) => void)(message, ...(args || [])); |
48 | | - } |
49 | | - }); |
50 | | - endGroup(); |
| 43 | +const logGroup = ( |
| 44 | + groupName: string, |
| 45 | + messages: { level: keyof typeof log; message: string; args?: any[] }[], |
| 46 | +) => { |
| 47 | + startGroup(groupName); |
| 48 | + messages.forEach(({ level, message, args }) => { |
| 49 | + if (typeof log[level] === "function") { |
| 50 | + createLoggerFunction(log[level] as (...args: any[]) => void)( |
| 51 | + message, |
| 52 | + ...(args || []), |
| 53 | + ); |
| 54 | + } |
| 55 | + }); |
| 56 | + endGroup(); |
51 | 57 | }; |
52 | 58 |
|
53 | 59 | // Custom FetchDebug logger |
54 | 60 | const fetchDebug = createLoggerFunction((...args: any[]) => { |
55 | | - const a = log.getLevel(); |
56 | | - if (a <= log.levels.DEBUG) { |
57 | | - console.debug(...args); |
58 | | - } |
| 61 | + const a = log.getLevel(); |
| 62 | + if (a <= log.levels.DEBUG) { |
| 63 | + console.debug(...args); |
| 64 | + } |
59 | 65 | }); |
60 | 66 |
|
61 | 67 | export const Logger = { |
62 | | - debug: createLoggerFunction(log.debug), |
63 | | - endGroup, |
64 | | - error: createLoggerFunction(log.error), |
65 | | - fetchDebug, |
66 | | - info: createLoggerFunction(log.info), |
67 | | - logGroup, |
68 | | - startGroup, |
69 | | - warn: createLoggerFunction(log.warn) |
| 68 | + debug: createLoggerFunction(log.debug), |
| 69 | + endGroup, |
| 70 | + error: createLoggerFunction(log.error), |
| 71 | + fetchDebug, |
| 72 | + info: createLoggerFunction(log.info), |
| 73 | + logGroup, |
| 74 | + startGroup, |
| 75 | + warn: createLoggerFunction(log.warn), |
70 | 76 | }; |
0 commit comments