Skip to content

Commit

Permalink
refactor: using stream to maintain pendingLogs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Jul 26, 2024
1 parent 6b36196 commit c993d46
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions lib/main/logs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,33 @@ const CollectLogsHook = {
hostname: null,
pid,
pendingLogs: [],
isInitializationFailed: false,
enableConsoleOutput: process.env.NODE_ENV !== 'production'
};

const timeoutId = setTimeout(() => {
CollectLogsHook.addEmptyStreamToLogs();
CollectLogsHook.isInitializationFailed = true;
console.error('Error: Unable to initialize the errsole');
}, 10000);

CollectLogsHook.initialize = function (options) {
const self = this;
this.storage = options.storage;
this.hostname = options.serverName || os.hostname();
if (options && typeof options.enableConsoleOutput !== 'undefined') {
this.enableConsoleOutput = options.enableConsoleOutput;
}
if (this.storage.once) {
this.storage.once('ready', () => {
clearTimeout(timeoutId);
if (this.isInitializationFailed) {
this.initializeLogStream();
this.isInitializationFailed = false;
} else {
this.logStream.uncork();
}
});
}
this.collectLogs = options.collectLogs || ['info', 'error'];
if (this.collectLogs.includes(LogLevel.INFO)) {
console.log('Errsole is capturing INFO logs.');
Expand All @@ -42,12 +54,6 @@ CollectLogsHook.initialize = function (options) {
process.stderr.write = originalStderrWrite;
console.log('Errsole is NOT capturing ERROR logs.');
}
if (self.storage.once) {
self.storage.once('ready', function () {
clearTimeout(timeoutId);
self.addStreamToLogs();
});
}
};

CollectLogsHook.captureLogs = function (level) {
Expand Down Expand Up @@ -87,7 +93,6 @@ CollectLogsHook.captureLogs = function (level) {
};
};

// Start capturing logs immediately with default settings
const originalStdoutWrite = process.stdout.write;
const originalStderrWrite = process.stderr.write;

Expand All @@ -106,28 +111,29 @@ CollectLogsHook.customLogger = function (level, message, metadata) {
} catch (err) { }
};

CollectLogsHook.logStream = new stream.Writable({
objectMode: true,
write (logEntry, encoding, callback) {
CollectLogsHook.pendingLogs.push(logEntry);
setImmediate(() => callback());
}
});

CollectLogsHook.addStreamToLogs = function () {
CollectLogsHook.initializeLogStream = function () {
this.logStream = new stream.Writable({
objectMode: true,
write (logEntry, encoding, callback) {
CollectLogsHook.storage.postLogs([logEntry]);
setImmediate(() => callback());
}
});
this.pendingLogs.forEach(logEntry => {
this.logStream.write(logEntry);
});
};

try {
CollectLogsHook.initializeLogStream();
if (CollectLogsHook.logStream) {
CollectLogsHook.logStream.cork();
} else {
throw new Error('Failed to initialize log stream');
}
} catch (error) {
console.error('Error initializing or corking log stream:', error);
}

CollectLogsHook.addEmptyStreamToLogs = function () {
this.logStream.destroy();
this.logStream = new stream.Writable({
objectMode: true,
write (logEntry, encoding, callback) {
Expand Down

0 comments on commit c993d46

Please sign in to comment.