logging-manager-tests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const SandboxedModule = require('sandboxed-module')
  2. const bunyan = require('bunyan')
  3. const { expect } = require('chai')
  4. const path = require('node:path')
  5. const sinon = require('sinon')
  6. const MODULE_PATH = path.join(__dirname, '../../logging-manager.js')
  7. describe('LoggingManager', function () {
  8. beforeEach(function () {
  9. this.start = Date.now()
  10. this.bunyanLogger = {
  11. addStream: sinon.stub(),
  12. debug: sinon.stub(),
  13. error: sinon.stub(),
  14. fatal: sinon.stub(),
  15. info: sinon.stub(),
  16. level: sinon.stub(),
  17. warn: sinon.stub(),
  18. }
  19. this.Bunyan = {
  20. createLogger: sinon.stub().returns(this.bunyanLogger),
  21. RingBuffer: bunyan.RingBuffer,
  22. }
  23. this.stackdriverStreamConfig = { stream: 'stackdriver' }
  24. this.stackdriverClient = {
  25. stream: sinon.stub().returns(this.stackdriverStreamConfig),
  26. }
  27. this.GCPLogging = {
  28. LoggingBunyan: sinon.stub().returns(this.stackdriverClient),
  29. }
  30. this.FileLogLevelChecker = {
  31. start: sinon.stub(),
  32. stop: sinon.stub(),
  33. }
  34. this.GCEMetadataLogLevelChecker = {
  35. start: sinon.stub(),
  36. stop: sinon.stub(),
  37. }
  38. this.LogLevelChecker = {
  39. FileLogLevelChecker: sinon.stub().returns(this.FileLogLevelChecker),
  40. GCEMetadataLogLevelChecker: sinon
  41. .stub()
  42. .returns(this.GCEMetadataLogLevelChecker),
  43. }
  44. this.LoggingManager = SandboxedModule.require(MODULE_PATH, {
  45. requires: {
  46. bunyan: this.Bunyan,
  47. './log-level-checker': this.LogLevelChecker,
  48. },
  49. })
  50. this.loggerName = 'test'
  51. this.logger = this.LoggingManager.initialize(this.loggerName)
  52. })
  53. afterEach(function () {
  54. this.LoggingManager.removeWarningHandler()
  55. })
  56. describe('initialize', function () {
  57. beforeEach(function () {
  58. this.Bunyan.createLogger.reset()
  59. })
  60. describe('not in production', function () {
  61. beforeEach(function () {
  62. this.logger = this.LoggingManager.initialize(this.loggerName)
  63. })
  64. it('should default to log level debug', function () {
  65. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  66. 'debug'
  67. )
  68. })
  69. it('should not instantiate a log level checker', function () {
  70. expect(this.LoggingManager.logLevelChecker).not.to.exist
  71. })
  72. })
  73. describe('in production', function () {
  74. beforeEach(function () {
  75. process.env.NODE_ENV = 'production'
  76. this.logger = this.LoggingManager.initialize(this.loggerName)
  77. })
  78. afterEach(function () {
  79. delete process.env.NODE_ENV
  80. })
  81. it('should default to log level info', function () {
  82. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  83. 'info'
  84. )
  85. })
  86. it('should set up a file log level checker', function () {
  87. expect(this.logger.logLevelChecker).to.equal(this.FileLogLevelChecker)
  88. expect(this.FileLogLevelChecker.start).to.have.been.called
  89. })
  90. })
  91. describe('when LOG_LEVEL set in env', function () {
  92. beforeEach(function () {
  93. process.env.LOG_LEVEL = 'trace'
  94. this.LoggingManager.initialize()
  95. })
  96. afterEach(function () {
  97. delete process.env.LOG_LEVEL
  98. })
  99. it('should use custom log level', function () {
  100. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  101. 'trace'
  102. )
  103. })
  104. })
  105. })
  106. describe('bunyan logging', function () {
  107. beforeEach(function () {
  108. this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
  109. })
  110. it('should log debug', function () {
  111. this.logger.debug(this.logArgs)
  112. this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
  113. })
  114. it('should log error', function () {
  115. this.logger.error(this.logArgs)
  116. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  117. })
  118. it('should log fatal', function () {
  119. this.logger.fatal(this.logArgs)
  120. this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
  121. })
  122. it('should log info', function () {
  123. this.logger.info(this.logArgs)
  124. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  125. })
  126. it('should log warn', function () {
  127. this.logger.warn(this.logArgs)
  128. this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
  129. })
  130. it('should log err', function () {
  131. this.logger.err(this.logArgs)
  132. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  133. })
  134. })
  135. describe('ringbuffer', function () {
  136. beforeEach(function () {
  137. this.logBufferMock = [
  138. { msg: 'log 1' },
  139. { msg: 'log 2' },
  140. { level: 50, msg: 'error' },
  141. ]
  142. })
  143. describe('when ring buffer size is positive', function () {
  144. beforeEach(function () {
  145. process.env.LOG_RING_BUFFER_SIZE = '20'
  146. this.logger = this.LoggingManager.initialize(this.loggerName)
  147. this.logger.ringBuffer.records = this.logBufferMock
  148. this.logger.error({}, 'error')
  149. })
  150. afterEach(function () {
  151. process.env.LOG_RING_BUFFER_SIZE = undefined
  152. })
  153. it('should include buffered logs in error log and filter out error logs in buffer', function () {
  154. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  155. { msg: 'log 1' },
  156. { msg: 'log 2' },
  157. ])
  158. })
  159. })
  160. describe('when ring buffer size is zero', function () {
  161. beforeEach(function () {
  162. process.env.LOG_RING_BUFFER_SIZE = '0'
  163. this.logger = this.LoggingManager.initialize(this.loggerName)
  164. this.logger.error({}, 'error')
  165. })
  166. afterEach(function () {
  167. process.env.LOG_RING_BUFFER_SIZE = undefined
  168. })
  169. it('should not include buffered logs in error log', function () {
  170. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  171. })
  172. })
  173. })
  174. })