-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathLog.ts
More file actions
42 lines (38 loc) · 1.33 KB
/
Log.ts
File metadata and controls
42 lines (38 loc) · 1.33 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
import { IS_SERVICE_WORKER, LOGGING } from '../utils/EnvVariables';
export default class Log {
private static shouldLog(): boolean {
if (IS_SERVICE_WORKER)
return !!(self as unknown as ServiceWorkerGlobalScope).shouldLog;
try {
/* LocalStorage may not be accessible on browser profiles that restrict 3rd party cookies */
const level = window.localStorage.getItem('loglevel');
return level?.toLowerCase() === 'trace';
} catch (e) {
return false;
}
}
/**
* Sets the log level for page context.
* Will not do anything in service worker context.
*/
public static setLevel(level: string) {
if (IS_SERVICE_WORKER) return;
/* LocalStorage may not be accessible on browser profiles that restrict 3rd party cookies */
try {
window.localStorage.setItem('loglevel', level);
} catch (e) {
console.error(e);
}
}
private static createLogMethod(consoleMethod: keyof Console) {
return (...args: unknown[]): void => {
if (LOGGING || this.shouldLog() || consoleMethod === 'error') {
(console[consoleMethod] as (...args: unknown[]) => void)(...args);
}
};
}
static debug = Log.createLogMethod('debug');
static info = Log.createLogMethod('info');
static warn = Log.createLogMethod('warn');
static error = Log.createLogMethod('error');
}