-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.ts
111 lines (89 loc) · 2.94 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
import * as vscode from "vscode";
import { FirecoderTelemetrySenderInstance } from "./telemetry";
import { LogLevel } from "@grafana/faro-core";
interface LogOptions {
component?: string;
sendTelemetry?: boolean;
}
class Log {
private outputChannel: vscode.LogOutputChannel;
private activePerfMarkers: Map<string, number> = new Map();
constructor() {
this.outputChannel = vscode.window.createOutputChannel("FireCoder", {
log: true,
});
}
public startPerfMarker(marker: string) {
const startTime = performance.now();
this.outputChannel.appendLine(`PERF_MARKER> Start ${marker}`);
this.activePerfMarkers.set(marker, startTime);
}
public endPerfMarker(marker: string) {
const endTime = performance.now();
this.outputChannel.appendLine(
`PERF_MARKER> End ${marker}: ${
endTime - this.activePerfMarkers.get(marker)!
} ms`
);
this.activePerfMarkers.delete(marker);
}
private logString(message: any, component?: string): string {
if (typeof message !== "string") {
if (message instanceof Error) {
message = message.message;
} else if ("toString" in message) {
message = message.toString();
} else {
message = JSON.stringify(message);
}
}
return component ? `${component}> ${message}` : message;
}
public trace(message: any, options?: LogOptions) {
const messageString = this.logString(message, options?.component);
this.outputChannel.trace(messageString);
if (options?.sendTelemetry) {
FirecoderTelemetrySenderInstance.sendLogText(messageString, {
level: LogLevel.TRACE,
});
}
}
public debug(message: any, options?: LogOptions) {
const messageString = this.logString(message, options?.component);
this.outputChannel.debug(messageString);
if (options?.sendTelemetry) {
FirecoderTelemetrySenderInstance.sendLogText(messageString, {
level: LogLevel.DEBUG,
});
}
}
public info(message: any, options?: LogOptions) {
const messageString = this.logString(message, options?.component);
this.outputChannel.info(messageString);
if (options?.sendTelemetry) {
FirecoderTelemetrySenderInstance.sendLogText(messageString, {
level: LogLevel.INFO,
});
}
}
public warn(message: any, options?: LogOptions) {
const messageString = this.logString(message, options?.component);
this.outputChannel.warn(messageString);
if (options?.sendTelemetry) {
FirecoderTelemetrySenderInstance.sendLogText(messageString, {
level: LogLevel.WARN,
});
}
}
public error(message: any, options?: LogOptions) {
const messageString = this.logString(message, options?.component);
this.outputChannel.error(messageString);
if (options?.sendTelemetry) {
FirecoderTelemetrySenderInstance.sendLogText(messageString, {
level: LogLevel.ERROR,
});
}
}
}
const Logger = new Log();
export default Logger;