Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use file streams instead
  • Loading branch information
msujew committed Feb 6, 2025
commit ce03bc4106f8999cd8aed1b5af46613747fabb81
29 changes: 14 additions & 15 deletions packages/core/src/node/console-logger-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { LogLevelCliContribution } from './logger-cli-contribution';
import { ILoggerServer, ILoggerClient, ConsoleLogger, rootLoggerName } from '../common/logger-protocol';
import { format } from 'util';
import { EOL } from 'os';
import * as fs from 'fs/promises';
import * as fs from 'fs';

@injectable()
export class ConsoleLoggerServer implements ILoggerServer {
Expand All @@ -33,7 +33,7 @@ export class ConsoleLoggerServer implements ILoggerServer {
@inject(LogLevelCliContribution)
protected cli: LogLevelCliContribution;

protected failedLogFileWrite = false;
protected logFileStream?: fs.WriteStream;

@postConstruct()
protected init(): void {
Expand Down Expand Up @@ -65,22 +65,21 @@ export class ConsoleLoggerServer implements ILoggerServer {
const configuredLogLevel = await this.getLogLevel(name);
if (logLevel >= configuredLogLevel) {
const fullMessage = ConsoleLogger.log(name, logLevel, message, params);
await this.logToFile(fullMessage, params);
this.logToFile(fullMessage, params);
}
}

protected async logToFile(message: string, params: any[]): Promise<void> {
if (this.cli.logFile) {
try {
const formatted = format(message, ...params) + EOL;
await fs.appendFile(this.cli.logFile, formatted);
} catch (error) {
// Avoid spamming the console with errors in case the log file is not writable
if (!this.failedLogFileWrite) {
console.error(`Failed to write to log file: ${error}`);
this.failedLogFileWrite = true;
}
}
protected logToFile(message: string, params: any[]): void {
if (this.cli.logFile && !this.logFileStream) {
this.logFileStream = fs.createWriteStream(this.cli.logFile, { flags: 'a' });
// Only log errors once to avoid spamming the console
this.logFileStream.once('error', error => {
console.error(`Error writing to log file ${this.cli.logFile}`, error);
});
}
if (this.logFileStream) {
const formatted = format(message, ...params) + EOL;
this.logFileStream.write(formatted);
}
}

Expand Down
Loading