logging-manager-tests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.fetchUtils = {
  45. setLogger: sinon.stub(),
  46. }
  47. this.LoggingManager = SandboxedModule.require(MODULE_PATH, {
  48. requires: {
  49. bunyan: this.Bunyan,
  50. '@overleaf/fetch-utils': this.fetchUtils,
  51. './log-level-checker': this.LogLevelChecker,
  52. },
  53. })
  54. this.loggerName = 'test'
  55. this.logger = this.LoggingManager.initialize(this.loggerName)
  56. })
  57. afterEach(function () {
  58. this.LoggingManager.removeWarningHandler()
  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(function () {
  83. delete process.env.NODE_ENV
  84. })
  85. it('should default to log level info', function () {
  86. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  87. 'info'
  88. )
  89. })
  90. it('should set up a file log level checker', function () {
  91. expect(this.logger.logLevelChecker).to.equal(this.FileLogLevelChecker)
  92. expect(this.FileLogLevelChecker.start).to.have.been.called
  93. })
  94. })
  95. describe('when LOG_LEVEL set in env', function () {
  96. beforeEach(function () {
  97. process.env.LOG_LEVEL = 'trace'
  98. this.LoggingManager.initialize()
  99. })
  100. afterEach(function () {
  101. delete process.env.LOG_LEVEL
  102. })
  103. it('should use custom log level', function () {
  104. this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
  105. 'trace'
  106. )
  107. })
  108. })
  109. })
  110. describe('bunyan logging', function () {
  111. beforeEach(function () {
  112. this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
  113. })
  114. it('should log debug', function () {
  115. this.logger.debug(this.logArgs)
  116. this.bunyanLogger.debug.should.have.been.calledWith(this.logArgs)
  117. })
  118. it('should log error', function () {
  119. this.logger.error(this.logArgs)
  120. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  121. })
  122. it('should log fatal', function () {
  123. this.logger.fatal(this.logArgs)
  124. this.bunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
  125. })
  126. it('should log info', function () {
  127. this.logger.info(this.logArgs)
  128. this.bunyanLogger.info.should.have.been.calledWith(this.logArgs)
  129. })
  130. it('should log warn', function () {
  131. this.logger.warn(this.logArgs)
  132. this.bunyanLogger.warn.should.have.been.calledWith(this.logArgs)
  133. })
  134. it('should log err', function () {
  135. this.logger.err(this.logArgs)
  136. this.bunyanLogger.error.should.have.been.calledWith(this.logArgs)
  137. })
  138. })
  139. describe('ringbuffer', function () {
  140. beforeEach(function () {
  141. this.logBufferMock = [
  142. { msg: 'log 1' },
  143. { msg: 'log 2' },
  144. { level: 50, msg: 'error' },
  145. ]
  146. })
  147. describe('when ring buffer size is positive', function () {
  148. beforeEach(function () {
  149. process.env.LOG_RING_BUFFER_SIZE = '20'
  150. this.logger = this.LoggingManager.initialize(this.loggerName)
  151. this.logger.ringBuffer.records = this.logBufferMock
  152. this.logger.error({}, 'error')
  153. })
  154. afterEach(function () {
  155. process.env.LOG_RING_BUFFER_SIZE = undefined
  156. })
  157. it('should include buffered logs in error log and filter out error logs in buffer', function () {
  158. this.bunyanLogger.error.lastCall.args[0].logBuffer.should.deep.equal([
  159. { msg: 'log 1' },
  160. { msg: 'log 2' },
  161. ])
  162. })
  163. })
  164. describe('when ring buffer size is zero', function () {
  165. beforeEach(function () {
  166. process.env.LOG_RING_BUFFER_SIZE = '0'
  167. this.logger = this.LoggingManager.initialize(this.loggerName)
  168. this.logger.error({}, 'error')
  169. })
  170. afterEach(function () {
  171. process.env.LOG_RING_BUFFER_SIZE = undefined
  172. })
  173. it('should not include buffered logs in error log', function () {
  174. expect(this.bunyanLogger.error.lastCall.args[0].logBuffer).be.undefined
  175. })
  176. })
  177. })
  178. })