/
logger.ts
130 lines (103 loc) · 3.76 KB
/
logger.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {LOG_LEVEL} from './const';
import {toDart} from './wrappers';
import {Package} from './entities';
import {IDartApi} from "./api/grok_api.g";
import dayjs from "dayjs";
const api: IDartApi = <any>window;
export type LogMessage = {level: LOG_LEVEL, message: string, params?: object, type?: string, stackTrace?: string};
type LoggerPutCallback = (logRecord: LogMessage) => void;
export class Logger {
putCallback?: LoggerPutCallback;
private readonly dart: any;
private static consoleLogs: object[] = [];
constructor(putCallback?: LoggerPutCallback, options?: {staticLogger?: boolean, params?: object}) {
this.putCallback = putCallback;
if (options?.staticLogger != true)
this.dart = api.grok_GetLogger(toDart(options?.params));
}
static create(options?: {params: object}) {
return new Logger(undefined, {params: options?.params});
}
static getStatic() {
return new Logger(undefined, {staticLogger: true});
}
static translateStackTrace(stackTrace: string): string {
return api.grok_Log_TranslateStackTrace(stackTrace);
}
/** @Obsolete, for backward compatibility, use {audit} instead **/
log(message: string, params: object, type: string = 'log'): void {
this._log({level: LOG_LEVEL.AUDIT, message, params, type});
}
/** Reports audit record to Datagrok **/
audit(message: string, params?: object, type: string = 'log'): void {
this._log({level: LOG_LEVEL.AUDIT, message, params, type});
}
/** Reports usage record to Datagrok **/
usage(message: string, params?: object, type: string = 'usage'): void {
this._log({level: LOG_LEVEL.USAGE, message, params, type});
}
/** Reports info record to Datagrok **/
info(message: string, params?: object): void {
this._log({level: LOG_LEVEL.INFO, message, params});
}
/** Reports debug record to Datagrok **/
debug(message: string, params?: object): void {
this._log({level: LOG_LEVEL.DEBUG, message, params});
}
/** Reports warning record to Datagrok **/
warning(message: string, params?: object): void {
this._log({level: LOG_LEVEL.WARNING, message, params});
}
/** Reports error record to Datagrok **/
error(message: any, params?: object, stackTrace?: string): void {
if (message instanceof Error) {
stackTrace ??= message.stack;
message = message.message;
}
this._log({level: LOG_LEVEL.ERROR, message, params, stackTrace});
}
_log(msg: LogMessage) {
if (this.putCallback != null)
this.putCallback(msg);
msg.stackTrace ??= new Error().stack;
api.grok_Log(this.dart, msg.level, msg.message, toDart(msg.params), msg.type, msg.stackTrace);
}
static getConsoleOutput(): string {
return JSON.stringify(this.consoleLogs, null, 2);
}
static interceptConsoleOutput(): void {
const intercept = (f: (message?: any, ...optionalParams: any[]) => void) => {
const std = f.bind(console);
return (...args: any[]) => {
try { this.consoleLogs.push({'time': dayjs().utc().valueOf(),
'message': args.map((x) => `${x}`).join(' ')}); }
catch (_) {}
std(...args);
}
};
console.log = intercept(console.log);
console.info = intercept(console.info);
console.warn = intercept(console.warn);
console.error = intercept(console.error);
}
}
export class PackageLogger extends Logger {
private package: Package;
constructor(_package: Package) {
super();
this.package = _package;
}
_log(msg: LogMessage) {
msg.params ??= {};
//@ts-ignore
msg.params['package'] = this.package.dart;
//@ts-ignore
msg.params['packageName'] = this.package.name;
super._log(msg);
}
}
export class DetailedLog {
static async getAccordion(reportId: string): Promise<HTMLElement> {
return api.grok_DetailedLog_Get_Accordion(reportId);
}
}