|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | import { Logger as VSCodeLogger } from 'vscode-jsonrpc';
|
11 |
| -import { DiagnosticSeverity } from 'vscode-languageserver'; |
12 |
| - |
13 |
| -import * as fs from 'node:fs'; |
14 |
| -import * as os from 'node:os'; |
15 |
| -import { join } from 'node:path'; |
16 |
| -import { Socket } from 'node:net'; |
17 |
| - |
18 |
| -import { |
19 |
| - DIAGNOSTIC_SEVERITY, |
20 |
| - SeverityEnum, |
21 |
| - SEVERITY, |
22 |
| -} from 'graphql-language-service'; |
| 11 | +import { Connection } from 'vscode-languageserver'; |
23 | 12 |
|
24 | 13 | export class Logger implements VSCodeLogger {
|
25 |
| - _logFilePath: string; |
26 |
| - _stderrOnly: boolean; |
27 |
| - |
28 |
| - constructor(tmpDir?: string, stderrOnly?: boolean) { |
29 |
| - const dir = join(tmpDir || os.tmpdir(), 'graphql-language-service-logs'); |
30 |
| - try { |
31 |
| - if (!fs.existsSync(dir)) { |
32 |
| - fs.mkdirSync(dir); |
33 |
| - } |
34 |
| - } catch { |
35 |
| - // intentionally no-op. Don't block the language server even if |
36 |
| - // the necessary setup cannot be completed for logger. |
37 |
| - } |
38 |
| - |
39 |
| - this._logFilePath = join( |
40 |
| - dir, |
41 |
| - `graphql-language-service-log-${ |
42 |
| - os.userInfo().username |
43 |
| - }-${getDateString()}.log`, |
44 |
| - ); |
45 |
| - |
46 |
| - this._stderrOnly = stderrOnly || false; |
47 |
| - } |
| 14 | + constructor(private _connection: Connection) {} |
48 | 15 |
|
49 | 16 | error(message: string): void {
|
50 |
| - this._log(message, SEVERITY.Error); |
| 17 | + this._connection.console.error(message); |
51 | 18 | }
|
52 | 19 |
|
53 | 20 | warn(message: string): void {
|
54 |
| - this._log(message, SEVERITY.Warning); |
| 21 | + this._connection.console.warn(message); |
55 | 22 | }
|
56 | 23 |
|
57 | 24 | info(message: string): void {
|
58 |
| - this._log(message, SEVERITY.Information); |
| 25 | + this._connection.console.info(message); |
59 | 26 | }
|
60 | 27 |
|
61 | 28 | log(message: string): void {
|
62 |
| - this._log(message, SEVERITY.Hint); |
63 |
| - } |
64 |
| - |
65 |
| - _log(message: string, severityKey: SeverityEnum): void { |
66 |
| - const timestamp = new Date().toLocaleString(); |
67 |
| - const severity = DIAGNOSTIC_SEVERITY[severityKey]; |
68 |
| - const { pid } = process; |
69 |
| - |
70 |
| - const stringMessage = String(message).trim(); |
71 |
| - const logMessage = `${timestamp} [${severity}] (pid: ${pid}) graphql-language-service-usage-logs: ${stringMessage}\n`; |
72 |
| - // write to the file in tmpdir |
73 |
| - fs.appendFile(this._logFilePath, logMessage, _error => {}); |
74 |
| - // @TODO: enable with debugging |
75 |
| - if (severityKey !== SEVERITY.Hint) { |
76 |
| - this._getOutputStream(severity).write(logMessage, err => { |
77 |
| - if (err) { |
78 |
| - // eslint-disable-next-line no-console |
79 |
| - console.error(err); |
80 |
| - } |
81 |
| - }); |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - _getOutputStream(severity: DiagnosticSeverity): Socket { |
86 |
| - if (this._stderrOnly || severity === DIAGNOSTIC_SEVERITY.Error) { |
87 |
| - return process.stderr; |
88 |
| - } |
89 |
| - |
90 |
| - return process.stdout; |
| 29 | + this._connection.console.log(message); |
91 | 30 | }
|
92 | 31 | }
|
93 | 32 |
|
94 |
| -// function getUnixTime() { |
95 |
| -// return new Date().getTime() / 1000; |
96 |
| -// } |
97 |
| - |
98 |
| -function getDateString() { |
99 |
| - const date = new Date(); |
100 |
| - return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; |
| 33 | +export class NoopLogger implements VSCodeLogger { |
| 34 | + error() {} |
| 35 | + warn() {} |
| 36 | + info() {} |
| 37 | + log() {} |
101 | 38 | }
|
0 commit comments