Skip to content

Commit 16b2c42

Browse files
kyliauKeen Yee Liau
authored andcommitted
refactor: remove traceToConsole feature in Logger
Currently if users ask for logging to disabled (by not providing a filename for the log file), the Logger defaults to writing to console. In practice, this feature is not used at all. If users do not want logging, then we should just disable logging entirely.
1 parent bf6101a commit 16b2c42

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

server/src/logger.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface LoggerOptions {
3838
* Create a logger instance to write to file.
3939
* @param options Logging options.
4040
*/
41-
export function createLogger(options: LoggerOptions): Logger {
41+
export function createLogger(options: LoggerOptions): ts.server.Logger {
4242
let logLevel: ts.server.LogLevel;
4343
switch (options.logVerbosity) {
4444
case 'requestTime':
@@ -55,9 +55,7 @@ export function createLogger(options: LoggerOptions): Logger {
5555
logLevel = ts.server.LogLevel.terse;
5656
break;
5757
}
58-
// If logFile is not provided then just trace to console.
59-
const traceToConsole = !options.logFile;
60-
return new Logger(traceToConsole, logLevel, options.logFile);
58+
return new Logger(logLevel, options.logFile);
6159
}
6260

6361
// TODO: Code below is from TypeScript's repository. Maybe create our own
@@ -72,17 +70,17 @@ function nowString() {
7270
return `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${d.getMilliseconds()}`;
7371
}
7472

75-
export class Logger implements ts.server.Logger {
76-
private fd = -1;
73+
class Logger implements ts.server.Logger {
74+
private readonly fd: number;
7775
private seq = 0;
7876
private inGroup = false;
7977
private firstInGroup = true;
8078

8179
constructor(
82-
private readonly traceToConsole: boolean,
8380
private readonly level: ts.server.LogLevel,
8481
private readonly logFilename?: string,
8582
) {
83+
this.fd = -1;
8684
if (logFilename) {
8785
try {
8886
const dir = path.dirname(logFilename);
@@ -101,7 +99,7 @@ export class Logger implements ts.server.Logger {
10199
}
102100

103101
close() {
104-
if (this.fd >= 0) {
102+
if (this.loggingEnabled()) {
105103
fs.close(this.fd, noop);
106104
}
107105
}
@@ -118,10 +116,6 @@ export class Logger implements ts.server.Logger {
118116
this.msg(s, ts.server.Msg.Info);
119117
}
120118

121-
err(s: string) {
122-
this.msg(s, ts.server.Msg.Err);
123-
}
124-
125119
startGroup() {
126120
this.inGroup = true;
127121
this.firstInGroup = true;
@@ -132,39 +126,29 @@ export class Logger implements ts.server.Logger {
132126
}
133127

134128
loggingEnabled() {
135-
return !!this.logFilename || this.traceToConsole;
129+
return this.fd >= 0;
136130
}
137131

138132
hasLevel(level: ts.server.LogLevel) {
139133
return this.loggingEnabled() && this.level >= level;
140134
}
141135

142136
msg(s: string, type: ts.server.Msg = ts.server.Msg.Err) {
143-
if (!this.canWrite) return;
137+
if (!this.loggingEnabled()) {
138+
return;
139+
}
144140

145141
s = `[${nowString()}] ${s}\n`;
146142
if (!this.inGroup || this.firstInGroup) {
147143
const prefix = Logger.padStringRight(type + ' ' + this.seq.toString(), ' ');
148144
s = prefix + s;
149145
}
150-
this.write(s);
151-
if (!this.inGroup) {
152-
this.seq++;
153-
}
154-
}
155146

156-
private get canWrite() {
157-
return this.fd >= 0 || this.traceToConsole;
158-
}
147+
const buf = Buffer.from(s);
148+
fs.writeSync(this.fd, buf, 0, buf.length);
159149

160-
private write(s: string) {
161-
if (this.fd >= 0) {
162-
const buf = Buffer.from(s);
163-
// tslint:disable-next-line no-null-keyword
164-
fs.writeSync(this.fd, buf, 0, buf.length, /*position*/ null!); // TODO: GH#18217
165-
}
166-
if (this.traceToConsole) {
167-
console.warn(s);
150+
if (!this.inGroup) {
151+
this.seq++;
168152
}
169153
}
170154
}

server/src/session.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ import * as notification from '../../common/out/notifications';
1313

1414
import {tsCompletionEntryToLspCompletionItem} from './completion';
1515
import {tsDiagnosticToLspDiagnostic} from './diagnostic';
16-
import {Logger} from './logger';
1716
import {ServerHost} from './server_host';
1817
import {filePathToUri, lspPositionToTsPosition, lspRangeToTsPositions, tsTextSpanToLspRange, uriToFilePath} from './utils';
1918

2019
export interface SessionOptions {
2120
host: ServerHost;
22-
logger: Logger;
21+
logger: ts.server.Logger;
2322
ngPlugin: string;
2423
ngProbeLocation: string;
2524
ivy: boolean;
@@ -40,7 +39,7 @@ const EMPTY_RANGE = lsp.Range.create(0, 0, 0, 0);
4039
export class Session {
4140
private readonly connection: lsp.IConnection;
4241
private readonly projectService: ts.server.ProjectService;
43-
private readonly logger: Logger;
42+
private readonly logger: ts.server.Logger;
4443
private readonly ivy: boolean;
4544
private diagnosticsTimeout: NodeJS.Timeout|null = null;
4645
private isProjectLoading = false;

0 commit comments

Comments
 (0)