Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 4df601b

Browse files
committed
add enum of log level
1 parent 4e753e6 commit 4df601b

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/logger/logger.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import * as winston from "winston";
66
import TelemetryTransport from "./telemetry-transport";
77
import UserNotificationTransport from "./user-notification-transport";
88

9+
export enum LogLevel {
10+
info = "info",
11+
warn = "warn",
12+
error = "error",
13+
}
14+
915
function FilterErrorPath(line: string): string {
1016
if (line) {
1117
const values: string[] = line.split("/out/");
@@ -21,9 +27,9 @@ function FilterErrorPath(line: string): string {
2127
export function configure(context: vscode.ExtensionContext) {
2228
winston.configure({
2329
transports: [
24-
new (winston.transports.File)({ level: "warn", filename: context.asAbsolutePath("arduino.log") }),
25-
new TelemetryTransport({ level: "info", context }),
26-
new UserNotificationTransport({ level: "info" }),
30+
new (winston.transports.File)({ level: LogLevel.warn, filename: context.asAbsolutePath("arduino.log") }),
31+
new TelemetryTransport({ level: LogLevel.info, context }),
32+
new UserNotificationTransport({ level: LogLevel.info }),
2733
],
2834
});
2935
}
@@ -54,7 +60,7 @@ export function silly(message: string, metadata?: any) {
5460

5561
export function traceUserData(message: string, metadata?: any) {
5662
// use `info` as the log level and add a special flag in metadata
57-
winston.log("info", message, { ...metadata, telemetry: true });
63+
winston.log(LogLevel.info, message, { ...metadata, telemetry: true });
5864
}
5965

6066
function traceErrorOrWarning(level: string, message: string, error: Error, metadata?: any) {
@@ -68,15 +74,15 @@ function traceErrorOrWarning(level: string, message: string, error: Error, metad
6874
firstLine = FilterErrorPath(firstLine ? firstLine.replace(/\\/g, "/") : "");
6975
}
7076
}
71-
winston.log(level, "error", { ...metadata, message: error.message, errorLine: firstLine, telemetry: true });
77+
winston.log(level, message, { ...metadata, message: error.message, errorLine: firstLine, telemetry: true });
7278
}
7379

7480
export function traceError(message: string, error: Error, metadata?: any) {
75-
traceErrorOrWarning("error", message, error, metadata);
81+
traceErrorOrWarning(LogLevel.error, message, error, metadata);
7682
}
7783

7884
export function traceWarning(message: string, error: Error, metadata?: any) {
79-
traceErrorOrWarning("warn", message, error, metadata);
85+
traceErrorOrWarning(LogLevel.warn, message, error, metadata);
8086
}
8187

8288
export function notifyAndThrowUserError(errorCode: string, error: Error, message?: string) {

src/logger/telemetry-transport.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as vscode from "vscode";
55
import TelemetryReporter from "vscode-extension-telemetry";
66
import * as winston from "winston";
7+
import { LogLevel } from "./logger";
78
interface IPackageInfo {
89
name: string;
910
version: string;
@@ -60,7 +61,7 @@ export class TelemetryTransport extends winston.Transport {
6061
}
6162
}
6263
}
63-
if (level === "info") {
64+
if (level === LogLevel.info) {
6465
this.reporter.sendTelemetryEvent(message, properties, measures);
6566
} else {
6667
this.reporter.sendTelemetryErrorEvent(message, properties, measures, ["message", "notification", "errorLine"]);

src/logger/user-notification-transport.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import * as vscode from "vscode";
55
import * as winston from "winston";
6+
import { LogLevel } from "./logger";
67

78
export default class UserNotificationTransport extends winston.Transport {
89

@@ -13,9 +14,9 @@ export default class UserNotificationTransport extends winston.Transport {
1314
protected log(level: string, message: string, metadata?: any, callback?: (arg1, arg2) => void) {
1415
if (metadata && metadata.showUser) {
1516
const notification = (metadata && metadata.notification) ? metadata.notification : message;
16-
if (level === "warn") {
17+
if (level === LogLevel.warn) {
1718
vscode.window.showWarningMessage(notification);
18-
} else if (level === "error") {
19+
} else if (level === LogLevel.error) {
1920
vscode.window.showErrorMessage(notification);
2021
} else {
2122
winston.error(`Invalid error level '${level}' for user notification.`);

0 commit comments

Comments
 (0)