|
1 | | -import { inspect } from 'node:util'; |
| 1 | +import { formatWithOptions } from 'node:util'; |
2 | 2 | import vscode from 'vscode'; |
3 | | -import { getConfigValue, type LogLevel } from './config'; |
4 | 3 |
|
5 | 4 | function formatValues(values: unknown[]): string { |
6 | | - return values |
7 | | - .map((value) => |
8 | | - typeof value === 'string' |
9 | | - ? value |
10 | | - : inspect(value, { depth: 4, colors: false }), |
11 | | - ) |
12 | | - .join(' '); |
| 5 | + return formatWithOptions({ depth: 4 }, ...values); |
13 | 6 | } |
14 | 7 |
|
15 | 8 | export class Logger implements vscode.Disposable { |
16 | | - private readonly channel: vscode.OutputChannel; |
17 | | - private readonly disposables: vscode.Disposable[] = []; |
18 | | - private level: LogLevel; |
| 9 | + readonly #channel: vscode.LogOutputChannel; |
19 | 10 |
|
20 | 11 | constructor(private readonly name = 'Rstest') { |
21 | | - this.channel = vscode.window.createOutputChannel(this.name); |
22 | | - this.level = this.readLevel(); |
23 | | - this.disposables.push( |
24 | | - vscode.workspace.onDidChangeConfiguration((event) => { |
25 | | - if (event.affectsConfiguration('rstest.logLevel')) { |
26 | | - this.level = this.readLevel(); |
27 | | - } |
28 | | - }), |
29 | | - ); |
| 12 | + this.#channel = vscode.window.createOutputChannel(this.name, { log: true }); |
30 | 13 | } |
31 | 14 |
|
32 | | - public debug(...values: unknown[]) { |
33 | | - if (this.level !== 'debug') { |
34 | | - return; |
35 | | - } |
| 15 | + public trace(...values: unknown[]) { |
| 16 | + this.#channel.trace(formatValues(values)); |
| 17 | + } |
36 | 18 |
|
37 | | - this.write('DEBUG', values); |
| 19 | + public debug(...values: unknown[]) { |
| 20 | + this.#channel.debug(formatValues(values)); |
38 | 21 | } |
39 | 22 |
|
40 | 23 | public info(...values: unknown[]) { |
41 | | - this.write('INFO', values); |
| 24 | + this.#channel.info(formatValues(values)); |
42 | 25 | } |
43 | 26 |
|
44 | 27 | public warn(...values: unknown[]) { |
45 | | - this.write('WARN', values); |
| 28 | + this.#channel.warn(formatValues(values)); |
46 | 29 | } |
47 | 30 |
|
48 | 31 | public error(...values: unknown[]) { |
49 | | - this.write('ERROR', values); |
| 32 | + this.#channel.error(formatValues(values)); |
50 | 33 | } |
51 | 34 |
|
52 | 35 | public dispose() { |
53 | | - this.disposables.forEach((disposable) => { |
54 | | - disposable.dispose(); |
55 | | - }); |
56 | | - this.channel.dispose(); |
57 | | - } |
58 | | - |
59 | | - private readLevel(): LogLevel { |
60 | | - return getConfigValue('logLevel'); |
61 | | - } |
62 | | - |
63 | | - private write(tag: string, values: unknown[]) { |
64 | | - const timestamp = new Date().toISOString(); |
65 | | - const message = formatValues(values); |
66 | | - this.channel.appendLine(`[${timestamp}] [${tag}] ${message}`); |
| 36 | + this.#channel.dispose(); |
67 | 37 | } |
68 | 38 | } |
69 | 39 |
|
|
0 commit comments