-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
Copy pathLoggingService.ts
113 lines (101 loc) · 2.89 KB
/
LoggingService.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
import { window } from "vscode";
type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR" | "NONE";
export class LoggingService {
private outputChannel = window.createOutputChannel("Prettier");
private logLevel: LogLevel = "INFO";
public setOutputLevel(logLevel: LogLevel) {
this.logLevel = logLevel;
}
/**
* Append messages to the output channel and format it with a title
*
* @param message The message to append to the output channel
*/
public logDebug(message: string, data?: unknown): void {
if (
this.logLevel === "NONE" ||
this.logLevel === "INFO" ||
this.logLevel === "WARN" ||
this.logLevel === "ERROR"
) {
return;
}
this.logMessage(message, "DEBUG");
if (data) {
this.logObject(data);
}
}
/**
* Append messages to the output channel and format it with a title
*
* @param message The message to append to the output channel
*/
public logInfo(message: string, data?: unknown): void {
if (
this.logLevel === "NONE" ||
this.logLevel === "WARN" ||
this.logLevel === "ERROR"
) {
return;
}
this.logMessage(message, "INFO");
if (data) {
this.logObject(data);
}
}
/**
* Append messages to the output channel and format it with a title
*
* @param message The message to append to the output channel
*/
public logWarning(message: string, data?: unknown): void {
if (this.logLevel === "NONE" || this.logLevel === "ERROR") {
return;
}
this.logMessage(message, "WARN");
if (data) {
this.logObject(data);
}
}
public logError(message: string, error?: unknown) {
if (this.logLevel === "NONE") {
return;
}
this.logMessage(message, "ERROR");
if (typeof error === "string") {
// Errors as a string usually only happen with
// plugins that don't return the expected error.
this.outputChannel.appendLine(error);
} else if (error instanceof Error) {
if (error?.message) {
this.logMessage(error.message, "ERROR");
}
if (error?.stack) {
this.outputChannel.appendLine(error.stack);
}
} else if (error) {
this.logObject(error);
}
}
public show() {
this.outputChannel.show();
}
private logObject(data: unknown): void {
// const message = JSON.parser
// .format(JSON.stringify(data, null, 2), {
// parser: "json",
// })
// .trim();
const message = JSON.stringify(data, null, 2); // dont use prettier to keep it simple
this.outputChannel.appendLine(message);
}
/**
* Append messages to the output channel and format it with a title
*
* @param message The message to append to the output channel
*/
private logMessage(message: string, logLevel: LogLevel): void {
const title = new Date().toLocaleTimeString();
this.outputChannel.appendLine(`["${logLevel}" - ${title}] ${message}`);
}
}