-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathlogger.ts
More file actions
64 lines (52 loc) · 1.66 KB
/
logger.ts
File metadata and controls
64 lines (52 loc) · 1.66 KB
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
63
64
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { isUndefined } from '@lwc/shared';
import { getComponentStack } from './format';
import type { VM } from '../framework/vm';
const alreadyLoggedMessages = new Set();
// Only used in LWC's Karma tests
if (process.env.NODE_ENV === 'test-karma-lwc') {
(window as any).__lwcResetAlreadyLoggedMessages = () => {
alreadyLoggedMessages.clear();
};
}
function log(method: 'warn' | 'error', message: string, vm: VM | undefined, once: boolean) {
let msg = `[LWC ${method}]: ${message}`;
if (!isUndefined(vm)) {
msg = `${msg}\n${getComponentStack(vm)}`;
}
if (once) {
if (alreadyLoggedMessages.has(msg)) {
return;
}
alreadyLoggedMessages.add(msg);
}
// In Vitest tests, reduce the warning and error verbosity by not printing the callstack
if (process.env.NODE_ENV === 'test') {
/* eslint-disable-next-line no-console */
console[method](msg);
return;
}
try {
throw new Error(msg);
} catch (e) {
/* eslint-disable-next-line no-console */
console[method](e);
}
}
export function logError(message: string, vm?: VM) {
log('error', message, vm, false);
}
export function logErrorOnce(message: string, vm?: VM) {
log('error', message, vm, true);
}
export function logWarn(message: string, vm?: VM) {
log('warn', message, vm, false);
}
export function logWarnOnce(message: string, vm?: VM) {
log('warn', message, vm, true);
}