From cb22e98766b080ea9640c4380a70b9677afebeb1 Mon Sep 17 00:00:00 2001 From: Ersun Warncke Date: Mon, 11 Mar 2019 10:27:35 -0400 Subject: [PATCH] default ring buffer size to zero --- libraries/logger/logging-manager.js | 36 +++++++++++-------- .../logger/test/unit/loggingManagerTests.js | 14 +++++--- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/libraries/logger/logging-manager.js b/libraries/logger/logging-manager.js index 63fb9fa942..92a8f88171 100644 --- a/libraries/logger/logging-manager.js +++ b/libraries/logger/logging-manager.js @@ -8,23 +8,28 @@ const Logger = module.exports = { this.defaultLevel = process.env['LOG_LEVEL'] || (this.isProduction ? 'warn' : 'debug') this.loggerName = name - this.ringBuffer = new bunyan.RingBuffer({ - limit: process.env['LOG_RING_BUFFER_SIZE'] || 30 - }) + this.ringBufferSize = parseInt(process.env['LOG_RING_BUFFER_SIZE']) || 0 + const loggerStreams = [ + { + level: this.defaultLevel, + stream: process.stdout + } + ] + if (this.ringBufferSize > 0) { + this.ringBuffer = new bunyan.RingBuffer({limit: this.ringBufferSize}) + loggerStreams.push({ + level: 'trace', + type: 'raw', + stream: this.ringBuffer + }) + } + else { + this.ringBuffer = null + } this.logger = bunyan.createLogger({ name, serializers: bunyan.stdSerializers, - streams: [ - { - level: this.defaultLevel, - stream: process.stdout - }, - { - level: 'trace', - type: 'raw', - stream: this.ringBuffer - } - ] + streams: loggerStreams }) if (this.isProduction) { // clear interval if already set @@ -51,6 +56,7 @@ const Logger = module.exports = { } request(options, (err, response, body) => { if (err) { + this.logger.level(this.defaultLevel) return } if (parseInt(body) > Date.now()) { @@ -165,7 +171,7 @@ const Logger = module.exports = { }, error(attributes, message, ...args) { - if (this.isProduction) { + if (this.ringBuffer !== null) { attributes.logBuffer = this.ringBuffer.records } this.logger.error(attributes, message, ...Array.from(args)) diff --git a/libraries/logger/test/unit/loggingManagerTests.js b/libraries/logger/test/unit/loggingManagerTests.js index 7d1ec558a4..c45ef4e564 100644 --- a/libraries/logger/test/unit/loggingManagerTests.js +++ b/libraries/logger/test/unit/loggingManagerTests.js @@ -338,16 +338,16 @@ describe('LoggingManager', function() { ] }) - describe('in production', function() { + describe('when ring buffer size is positive', function() { beforeEach(function() { - process.env['NODE_ENV'] = 'production' + process.env['LOG_RING_BUFFER_SIZE'] = '20' this.logger = this.LoggingManager.initialize(this.loggerName) this.logger.ringBuffer.records = this.logBufferMock this.logger.error({}, 'error') }) afterEach(function() { - process.env['NODE_ENV'] = undefined + process.env['LOG_RING_BUFFER_SIZE'] = undefined }) it('should include buffered logs in error log', function() { @@ -357,13 +357,17 @@ describe('LoggingManager', function() { }) }) - describe('not in production', function() { + describe('when ring buffer size is zero', function() { beforeEach(function() { + process.env['LOG_RING_BUFFER_SIZE'] = '0' this.logger = this.LoggingManager.initialize(this.loggerName) - this.logger.ringBuffer.records = this.logBufferMock this.logger.error({}, 'error') }) + afterEach(function() { + process.env['LOG_RING_BUFFER_SIZE'] = undefined + }) + it('should not include buffered logs in error log', function() { chai.expect(this.mockBunyanLogger.error.lastCall.args[0].logBuffer).be .undefined