logging-manager-tests.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. '@google-cloud/logging-bunyan': this.GCPLogging,
  52. './log-level-checker': this.LogLevelChecker,
  53. './sentry-manager': sinon.stub().returns(this.SentryManager)
  54. }
  55. })
  56. this.loggerName = 'test'
  57. this.logger = this.LoggingManager.initialize(this.loggerName)
  58. this.logger.initializeErrorReporting('test_dsn')
  59. })
  60. describe('initialize', function () {
  61. beforeEach(function () {
  62. this.Bunyan.createLogger.reset()
  63. })
  64. describe('not in production', function () {
  65. beforeEach(function () {
  66. this.logger = this.LoggingManager.initialize(this.loggerName)
  67. })
  68. it('should default to log level debug', function () {
  69. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  70. 'debug'
  71. )
  72. })
  73. it('should not instantiate a log level checker', function () {
  74. expect(this.LoggingManager.logLevelChecker).not.to.exist
  75. })
  76. })
  77. describe('in production', function () {
  78. beforeEach(function () {
  79. process.env.NODE_ENV = 'production'
  80. this.logger = this.LoggingManager.initialize(this.loggerName)
  81. })
  82. afterEach(() => delete process.env.NODE_ENV)
  83. it('should default to log level warn', function () {
  84. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  85. 'warn'
  86. )
  87. })
  88. it('should set up a file log level checker', function () {
  89. expect(this.logger.logLevelChecker).to.equal(this.FileLogLevelChecker)
  90. expect(this.FileLogLevelChecker.start).to.have.been.called
  91. })
  92. })
  93. describe('when LOG_LEVEL set in env', function () {
  94. beforeEach(function () {
  95. process.env.LOG_LEVEL = 'trace'
  96. this.LoggingManager.initialize()
  97. })
  98. afterEach(() => delete process.env.LOG_LEVEL)
  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. it('should log log', function () {
  135. this.logger.log(this.logArgs)
  136. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  137. })
  138. })
  139. describe('logger.error', function () {
  140. it('should report errors to Sentry', function () {
  141. this.logger.error({ foo: 'bar' }, 'message')
  142. expect(this.SentryManager.captureExceptionRateLimited).to.have.been.called
  143. })
  144. })
  145. describe('ringbuffer', function () {
  146. beforeEach(function () {
  147. this.logBufferMock = [
  148. { msg: 'log 1' },
  149. { msg: 'log 2' },
  150. { level: 50, msg: 'error' }
  151. ]
  152. })
  153. describe('when ring buffer size is positive', function () {
  154. beforeEach(function () {
  155. process.env.LOG_RING_BUFFER_SIZE = '20'
  156. this.logger = this.LoggingManager.initialize(this.loggerName)
  157. this.logger.ringBuffer.records = this.logBufferMock
  158. this.logger.error({}, 'error')
  159. })
  160. afterEach(function () {
  161. process.env.LOG_RING_BUFFER_SIZE = undefined
  162. })
  163. it('should include buffered logs in error log and filter out error logs in buffer', function () {
  164. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  165. { msg: 'log 1' },
  166. { msg: 'log 2' }
  167. ])
  168. })
  169. })
  170. describe('when ring buffer size is zero', function () {
  171. beforeEach(function () {
  172. process.env.LOG_RING_BUFFER_SIZE = '0'
  173. this.logger = this.LoggingManager.initialize(this.loggerName)
  174. this.logger.error({}, 'error')
  175. })
  176. afterEach(function () {
  177. process.env.LOG_RING_BUFFER_SIZE = undefined
  178. })
  179. it('should not include buffered logs in error log', function () {
  180. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  181. })
  182. })
  183. })
  184. })