Skip to content

Commit

Permalink
refactor: removed extra write stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Jul 26, 2024
1 parent c993d46 commit 37ad834
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions lib/main/logs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,40 @@ CollectLogsHook.initialize = function (options) {

CollectLogsHook.captureLogs = function (level) {
const self = this;

const setWriteFunction = (writeFunction) => {
const originalWrite = writeFunction.write;
writeFunction.write = (chunk, encoding, done) => {
const cleanedChunk = stripAnsi(chunk.toString());
const logEntry = {
timestamp: new Date().toISOString(),
message: cleanedChunk,
source: 'console',
level,
hostname: self.hostname || '',
pid: self.pid || 0
};
self.logStream.write(logEntry);
if (self.enableConsoleOutput) {
originalWrite.call(writeFunction, chunk, encoding, done);
} else if (done) {
done(); // Ensure callback is called if provided
}
};
};

if (self.storage.once) {
self.storage.once('ready', function () {
if (!self.enableConsoleOutput) {
const writeFunction = level === LogLevel.INFO ? process.stdout : process.stderr;
writeFunction.write = function () {
try {
const argsArray = Array.from(arguments);
logBuffer.write.apply(logBuffer, argsArray);
} catch (err) { }
};
setWriteFunction(level === LogLevel.INFO ? process.stdout : process.stderr);
}
});
}
const logBuffer = new stream.Writable();
logBuffer._write = (chunk, encoding, done) => {
try {
const cleanedChunk = stripAnsi(chunk.toString());
const logEntry = { timestamp: new Date().toISOString(), message: cleanedChunk, source: 'console', level, hostname: self.hostname || '', pid: self.pid || 0 };
self.logStream.write(logEntry);
done();
} catch (err) { }
};

const originalWrite = level === LogLevel.INFO ? process.stdout.write : process.stderr.write;
const writeFunction = level === LogLevel.INFO ? process.stdout : process.stderr;

writeFunction.write = function () {
try {
const argsArray = Array.from(arguments);
originalWrite.apply(writeFunction, argsArray);
logBuffer.write.apply(logBuffer, argsArray);
} catch (err) { }
};
setWriteFunction(level === LogLevel.INFO ? process.stdout : process.stderr);
};

// start capturing stdout, stderr and storing in logStream from app start
const originalStdoutWrite = process.stdout.write;
const originalStderrWrite = process.stderr.write;

Expand Down

0 comments on commit 37ad834

Please sign in to comment.