logging-manager-tests.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.SentryManager = {
  45. captureException: sinon.stub(),
  46. captureExceptionRateLimited: sinon.stub(),
  47. }
  48. this.LoggingManager = SandboxedModule.require(MODULE_PATH, {
  49. requires: {
  50. bunyan: this.Bunyan,
  51. './log-level-checker': this.LogLevelChecker,
  52. './sentry-manager': sinon.stub().returns(this.SentryManager),
  53. },
  54. })
  55. this.loggerName = 'test'
  56. this.logger = this.LoggingManager.initialize(this.loggerName)
  57. this.logger.initializeErrorReporting('test_dsn')
  58. })
  59. afterEach(function () {
  60. this.LoggingManager.removeWarningHandler()
  61. })
  62. describe('initialize', function () {
  63. beforeEach(function () {
  64. this.Bunyan.createLogger.reset()
  65. })
  66. describe('not in production', function () {
  67. beforeEach(function () {
  68. this.logger = this.LoggingManager.initialize(this.loggerName)
  69. })
  70. it('should default to log level debug', function () {
  71. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  72. 'debug'
  73. )
  74. })
  75. it('should not instantiate a log level checker', function () {
  76. expect(this.LoggingManager.logLevelChecker).not.to.exist
  77. })
  78. })
  79. describe('in production', function () {
  80. beforeEach(function () {
  81. process.env.NODE_ENV = 'production'
  82. this.logger = this.LoggingManager.initialize(this.loggerName)
  83. })
  84. afterEach(function () {
  85. delete process.env.NODE_ENV
  86. })
  87. it('should default to log level info', function () {
  88. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  89. 'info'
  90. )
  91. })
  92. it('should set up a file log level checker', function () {
  93. expect(this.logger.logLevelChecker).to.equal(this.FileLogLevelChecker)
  94. expect(this.FileLogLevelChecker.start).to.have.been.called
  95. })
  96. })
  97. describe('when LOG_LEVEL set in env', function () {
  98. beforeEach(function () {
  99. process.env.LOG_LEVEL = 'trace'
  100. this.LoggingManager.initialize()
  101. })
  102. afterEach(function () {
  103. delete process.env.LOG_LEVEL
  104. })
  105. it('should use custom log level', function () {
  106. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  107. 'trace'
  108. )
  109. })
  110. })
  111. })
  112. describe('bunyan logging', function () {
  113. beforeEach(function () {
  114. this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
  115. })
  116. it('should log debug', function () {
  117. this.logger.debug(this.logArgs)
  118. this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
  119. })
  120. it('should log error', function () {
  121. this.logger.error(this.logArgs)
  122. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  123. })
  124. it('should log fatal', function () {
  125. this.logger.fatal(this.logArgs)
  126. this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
  127. })
  128. it('should log info', function () {
  129. this.logger.info(this.logArgs)
  130. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  131. })
  132. it('should log warn', function () {
  133. this.logger.warn(this.logArgs)
  134. this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
  135. })
  136. it('should log err', function () {
  137. this.logger.err(this.logArgs)
  138. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  139. })
  140. })
  141. describe('logger.error', function () {
  142. it('should report errors to Sentry', function () {
  143. this.logger.error({ foo: 'bar' }, 'message')
  144. expect(this.SentryManager.captureExceptionRateLimited).to.have.been.called
  145. })
  146. })
  147. describe('ringbuffer', function () {
  148. beforeEach(function () {
  149. this.logBufferMock = [
  150. { msg: 'log 1' },
  151. { msg: 'log 2' },
  152. { level: 50, msg: 'error' },
  153. ]
  154. })
  155. describe('when ring buffer size is positive', function () {
  156. beforeEach(function () {
  157. process.env.LOG_RING_BUFFER_SIZE = '20'
  158. this.logger = this.LoggingManager.initialize(this.loggerName)
  159. this.logger.ringBuffer.records = this.logBufferMock
  160. this.logger.error({}, 'error')
  161. })
  162. afterEach(function () {
  163. process.env.LOG_RING_BUFFER_SIZE = undefined
  164. })
  165. it('should include buffered logs in error log and filter out error logs in buffer', function () {
  166. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  167. { msg: 'log 1' },
  168. { msg: 'log 2' },
  169. ])
  170. })
  171. })
  172. describe('when ring buffer size is zero', function () {
  173. beforeEach(function () {
  174. process.env.LOG_RING_BUFFER_SIZE = '0'
  175. this.logger = this.LoggingManager.initialize(this.loggerName)
  176. this.logger.error({}, 'error')
  177. })
  178. afterEach(function () {
  179. process.env.LOG_RING_BUFFER_SIZE = undefined
  180. })
  181. it('should not include buffered logs in error log', function () {
  182. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  183. })
  184. })
  185. })
  186. })