-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathworkerLogger.ts
More file actions
30 lines (28 loc) · 994 Bytes
/
workerLogger.ts
File metadata and controls
30 lines (28 loc) · 994 Bytes
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
import { parentPort } from 'node:worker_threads';
import type { CommonLogger, LogLevel } from '../models';
export function sendLog(level: LogLevel, items: unknown[]) {
parentPort?.postMessage({
status: 'log',
level,
message: items
.map(item =>
typeof item === 'object' &&
!(item instanceof RegExp) &&
!(item instanceof Date) &&
!(item instanceof Function) &&
!(item instanceof Error)
? JSON.stringify(item, null, 2)
: item?.toString(),
)
.join(' '),
});
}
export const workerLogger: CommonLogger = {
error: (...data: unknown[]) => sendLog('error', data),
warn: (...data: unknown[]) => sendLog('warn', data),
info: (...data: unknown[]) => sendLog('info', data),
log: (...data: unknown[]) => sendLog('log', data),
group: (...data: unknown[]) => sendLog('group', data),
groupEnd: () => sendLog('groupEnd', []),
groupCollapsed: (...data: unknown[]) => sendLog('groupCollapsed', data),
};