Skip to content

Commit

Permalink
refactor: errsole capturing logs before initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Jul 25, 2024
1 parent 8974d87 commit 6b36196
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/errsole.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const ErrsoleMain = require('./main');
const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');
const koaProxies = require('koa-proxies');
const h2o2 = require('@hapi/h2o2');
const util = require('util');
const http = require('http');
const ErrsoleMain = require('./main');

const Errsole = {
port: 8001
};
Expand Down
55 changes: 51 additions & 4 deletions lib/main/logs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,44 @@ const LogLevel = {

const CollectLogsHook = {
storage: {},
collectLogs: [],
collectLogs: ['info', 'error'],
hostname: null,
pid,
pendingLogs: [],
enableConsoleOutput: process.env.NODE_ENV !== 'production'
};

const timeoutId = setTimeout(() => {
CollectLogsHook.addEmptyStreamToLogs();
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;
}
this.collectLogs = options.collectLogs || ['info', 'error'];
if (this.collectLogs.includes(LogLevel.INFO)) {
this.captureLogs(LogLevel.INFO);
console.log('Errsole is capturing INFO logs.');
} else {
process.stdout.write = originalStdoutWrite;
console.log('Errsole is NOT capturing INFO logs.');
}
if (this.collectLogs.includes(LogLevel.ERROR)) {
this.captureLogs(LogLevel.ERROR);
console.log('Errsole is capturing ERROR logs.');
} else {
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 @@ -74,6 +87,18 @@ CollectLogsHook.captureLogs = function (level) {
};
};

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

if (CollectLogsHook.collectLogs.includes(LogLevel.INFO)) {
CollectLogsHook.captureLogs(LogLevel.INFO);
}

if (CollectLogsHook.collectLogs.includes(LogLevel.ERROR)) {
CollectLogsHook.captureLogs(LogLevel.ERROR);
}

CollectLogsHook.customLogger = function (level, message, metadata) {
try {
const logEntry = { timestamp: new Date().toISOString(), message, meta: metadata || '{}', source: 'errsole', level, hostname: this.hostname || '', pid: this.pid || 0 };
Expand All @@ -84,11 +109,33 @@ CollectLogsHook.customLogger = function (level, message, metadata) {
CollectLogsHook.logStream = new stream.Writable({
objectMode: true,
write (logEntry, encoding, callback) {
CollectLogsHook.storage.postLogs([logEntry]);
CollectLogsHook.pendingLogs.push(logEntry);
setImmediate(() => callback());
}
});

CollectLogsHook.addStreamToLogs = 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);
});
};

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

CollectLogsHook.clearLogsBeforeExit = async function (timeout = 5000) {
if (typeof this.storage.flushLogs === 'function') {
try {
Expand Down

0 comments on commit 6b36196

Please sign in to comment.