Skip to content

Commit

Permalink
Add tracing log level to auth provider, #94005
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane committed Mar 31, 2020
1 parent 563997c commit bf456b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
15 changes: 14 additions & 1 deletion extensions/vscode-account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@
"title": "%signOut%",
"category": "%displayName%"
}
]
],
"configuration": {
"title": "Microsoft Account",
"properties": {
"microsoftAccount.logLevel": {
"type": "string",
"enum": [
"info",
"trace"
],
"default": "info"
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
Expand Down
5 changes: 4 additions & 1 deletion extensions/vscode-account/src/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Keychain {

async setToken(token: string): Promise<void> {
try {
Logger.trace('Writing to keychain', token);
return await this.keytar.setPassword(SERVICE_ID, ACCOUNT_ID, token);
} catch (e) {
// Ignore
Expand All @@ -59,7 +60,9 @@ export class Keychain {

async getToken(): Promise<string | null | undefined> {
try {
return await this.keytar.getPassword(SERVICE_ID, ACCOUNT_ID);
const result = await this.keytar.getPassword(SERVICE_ID, ACCOUNT_ID);
Logger.trace('Reading from keychain', result);
return result;
} catch (e) {
// Ignore
Logger.error(`Getting token failed: ${e}`);
Expand Down
18 changes: 18 additions & 0 deletions extensions/vscode-account/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ import * as vscode from 'vscode';

type LogLevel = 'Trace' | 'Info' | 'Error';

enum Level {
Trace = 'trace',
Info = 'Info'
}

class Log {
private output: vscode.OutputChannel;
private level: Level;

constructor() {
this.output = vscode.window.createOutputChannel('Account');
this.level = vscode.workspace.getConfiguration('microsoftAccount').get('logLevel') || Level.Info;
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('microsoftAccount.logLevel')) {
this.level = vscode.workspace.getConfiguration('microsoftAccount').get('logLevel') || Level.Info;
}
});
}

private data2String(data: any): string {
Expand All @@ -32,6 +44,12 @@ class Log {
this.logLevel('Error', message, data);
}

public trace(message: string, data?: any): void {
if (this.level === Level.Trace) {
this.logLevel('Trace', message, data);
}
}

public logLevel(level: LogLevel, message: string, data?: any): void {
this.output.appendLine(`[${level} - ${this.now()}] ${message}`);
if (data) {
Expand Down

0 comments on commit bf456b6

Please sign in to comment.