logging-manager-tests.js 6.3 KB

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