mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 04:09:28 +00:00
Support log output channels for extensions (#161249)
* introduce log api in extension context * separate registering output vs log channel * Separate extension log channels in show logs command * add logging error to embedder logger * show extension log in the extension editor * configure log level per extension * change the order of log entries * introduce logger * align with output chanel * revert changes * fixes
This commit is contained in:
committed by
GitHub
parent
82431003f3
commit
35c7ee9d02
@@ -48,6 +48,9 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"enabledApiProposals": [
|
||||
"extensionLog"
|
||||
],
|
||||
"aiKey": "0c6ae279ed8443289764825290e4f9e2-1a736e7c-1324-4338-be46-fc2a58ae4d14-7255",
|
||||
"main": "./out/extension.js",
|
||||
"browser": "./dist/browser/extension.js",
|
||||
|
||||
@@ -6,53 +6,24 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { AuthProviderType } from '../github';
|
||||
|
||||
type LogLevel = 'Trace' | 'Info' | 'Error';
|
||||
|
||||
export class Log {
|
||||
private output: vscode.OutputChannel;
|
||||
private output: vscode.LogOutputChannel;
|
||||
|
||||
constructor(private readonly type: AuthProviderType) {
|
||||
const friendlyName = this.type === AuthProviderType.github ? 'GitHub' : 'GitHub Enterprise';
|
||||
this.output = vscode.window.createOutputChannel(`${friendlyName} Authentication`);
|
||||
this.output = vscode.window.createOutputChannel(`${friendlyName} Authentication`, { log: true });
|
||||
}
|
||||
|
||||
private data2String(data: any): string {
|
||||
if (data instanceof Error) {
|
||||
return data.stack || data.message;
|
||||
}
|
||||
if (data.success === false && data.message) {
|
||||
return data.message;
|
||||
}
|
||||
return data.toString();
|
||||
public trace(message: string): void {
|
||||
this.output.trace(message);
|
||||
}
|
||||
|
||||
public trace(message: string, data?: any): void {
|
||||
this.logLevel('Trace', message, data);
|
||||
public info(message: string): void {
|
||||
this.output.info(message);
|
||||
}
|
||||
|
||||
public info(message: string, data?: any): void {
|
||||
this.logLevel('Info', message, data);
|
||||
public error(message: string): void {
|
||||
this.output.error(message);
|
||||
}
|
||||
|
||||
public error(message: string, data?: any): void {
|
||||
this.logLevel('Error', message, data);
|
||||
}
|
||||
|
||||
public logLevel(level: LogLevel, message: string, data?: any): void {
|
||||
this.output.appendLine(`[${level} - ${this.now()}] ${message}`);
|
||||
if (data) {
|
||||
this.output.appendLine(this.data2String(data));
|
||||
}
|
||||
}
|
||||
|
||||
private now(): string {
|
||||
const now = new Date();
|
||||
return padLeft(now.getUTCHours() + '', 2, '0')
|
||||
+ ':' + padLeft(now.getMinutes() + '', 2, '0')
|
||||
+ ':' + padLeft(now.getUTCSeconds() + '', 2, '0') + '.' + now.getMilliseconds();
|
||||
}
|
||||
}
|
||||
|
||||
function padLeft(s: string, n: number, pad = ' ') {
|
||||
return pad.repeat(Math.max(0, n - s.length)) + s;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
"../../src/vscode-dts/vscode.d.ts"
|
||||
"../../src/vscode-dts/vscode.d.ts",
|
||||
"../../src/vscode-dts/vscode.proposed.extensionLog.d.ts"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user