forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileLogger.ts
More file actions
46 lines (37 loc) · 1.34 KB
/
fileLogger.ts
File metadata and controls
46 lines (37 loc) · 1.34 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { WriteStream } from 'fs-extra';
import * as util from 'util';
import { Disposable } from 'vscode-jsonrpc';
import { Arguments, ILogging } from './types';
import { getTimeForLogging } from './util';
function formatMessage(level?: string, ...data: Arguments): string {
return level
? `[${level.toUpperCase()} ${getTimeForLogging()}]: ${util.format(...data)}\r\n`
: `${util.format(...data)}\r\n`;
}
export class FileLogger implements ILogging, Disposable {
constructor(private readonly stream: WriteStream) {}
public traceLog(...data: Arguments): void {
this.stream.write(formatMessage(undefined, ...data));
}
public traceError(...data: Arguments): void {
this.stream.write(formatMessage('error', ...data));
}
public traceWarn(...data: Arguments): void {
this.stream.write(formatMessage('warn', ...data));
}
public traceInfo(...data: Arguments): void {
this.stream.write(formatMessage('info', ...data));
}
public traceVerbose(...data: Arguments): void {
this.stream.write(formatMessage('debug', ...data));
}
public dispose(): void {
try {
this.stream.close();
} catch (ex) {
/** do nothing */
}
}
}