forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
60 lines (51 loc) · 1.71 KB
/
logger.ts
File metadata and controls
60 lines (51 loc) · 1.71 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { OutputChannel, window } from 'vscode';
import * as is from './is';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
export default class Logger {
private _output: OutputChannel;
private get output(): OutputChannel {
if (!this._output) {
this._output = window.createOutputChannel(localize('channelName', 'TypeScript'));
}
return this._output;
}
private data2String(data: any): string {
if (data instanceof Error) {
if (is.string(data.stack)) {
return data.stack;
}
return (data as Error).message;
}
if (is.boolean(data.success) && !data.success && is.string(data.message)) {
return data.message;
}
if (is.string(data)) {
return data;
}
return data.toString();
}
public info(message: string, data?: any): void {
this.logLevel('Info', message, data);
}
public warn(message: string, data?: any): void {
this.logLevel('Warn', message, data);
}
public error(message: string, data?: any): void {
// See https://github.com/Microsoft/TypeScript/issues/10496
if (data && data.message === 'No content available.') {
return;
}
this.logLevel('Error', message, data);
}
public logLevel(level: string, message: string, data?: any): void {
this.output.appendLine(`[${level} - ${(new Date().toLocaleTimeString())}] ${message}`);
if (data) {
this.output.appendLine(this.data2String(data));
}
}
}