logging-manager.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const Stream = require('node:stream')
  2. const bunyan = require('bunyan')
  3. const GCPManager = require('./gcp-manager')
  4. const Serializers = require('./serializers')
  5. const {
  6. FileLogLevelChecker,
  7. GCEMetadataLogLevelChecker,
  8. } = require('./log-level-checker')
  9. const { setLogger } = require('@overleaf/fetch-utils')
  10. const LoggingManager = {
  11. /**
  12. * @param {string} name - The name of the logger
  13. */
  14. initialize(name, options = {}) {
  15. this.isProduction =
  16. (process.env.NODE_ENV || '').toLowerCase() === 'production'
  17. const isTest = (process.env.NODE_ENV || '').toLowerCase() === 'test'
  18. this.defaultLevel =
  19. process.env.LOG_LEVEL ||
  20. (this.isProduction ? 'info' : isTest ? 'fatal' : 'debug')
  21. this.loggerName = name
  22. this.logger = bunyan.createLogger({
  23. name,
  24. serializers: {
  25. err: Serializers.err,
  26. error: Serializers.err,
  27. req: Serializers.req,
  28. res: Serializers.res,
  29. },
  30. streams: options.streams ?? [this._getOutputStreamConfig()],
  31. })
  32. this._setupRingBuffer()
  33. this._setupLogLevelChecker()
  34. setLogger(this)
  35. return this
  36. },
  37. /**
  38. * @param {Record<string, any>|string} attributes - Attributes to log (nice serialization for err, req, res)
  39. * @param {string} [message] - Optional message
  40. * @signature `debug(attributes, message)`
  41. * @signature `debug(message)`
  42. */
  43. debug(attributes, message, ...args) {
  44. return this.logger.debug(attributes, message, ...args)
  45. },
  46. /**
  47. * @param {Record<string, any>|string} attributes - Attributes to log (nice serialization for err, req, res)
  48. * @param {string} [message]
  49. * @signature `info(attributes, message)`
  50. * @signature `info(message)`
  51. */
  52. info(attributes, message, ...args) {
  53. return this.logger.info(attributes, message, ...args)
  54. },
  55. /**
  56. * @param {Record<string, any>} attributes - Attributes to log (nice serialization for err, req, res)
  57. * @param {string} [message]
  58. */
  59. error(attributes, message, ...args) {
  60. if (this.ringBuffer !== null && Array.isArray(this.ringBuffer.records)) {
  61. attributes.logBuffer = this.ringBuffer.records.filter(function (record) {
  62. return record.level !== 50
  63. })
  64. }
  65. this.logger.error(attributes, message, ...Array.from(args))
  66. },
  67. /**
  68. * Alias to the error method.
  69. * @param {Record<string, any>} attributes - Attributes to log (nice serialization for err, req, res)
  70. * @param {string} [message]
  71. */
  72. err(attributes, message, ...args) {
  73. return this.error(attributes, message, ...args)
  74. },
  75. /**
  76. * @param {Record<string, any>|string} attributes - Attributes to log (nice serialization for err, req, res)
  77. * @param {string} [message]
  78. * @signature `warn(attributes, message)`
  79. * @signature `warn(message)`
  80. */
  81. warn(attributes, message, ...args) {
  82. return this.logger.warn(attributes, message, ...args)
  83. },
  84. /**
  85. * @param {Record<string, any>} attributes - Attributes to log (nice serialization for err, req, res)
  86. * @param {string} [message]
  87. */
  88. fatal(attributes, message) {
  89. this.logger.fatal(attributes, message)
  90. },
  91. _getOutputStreamConfig() {
  92. switch (process.env.LOGGING_FORMAT) {
  93. case 'gke': {
  94. const stream = new Stream.Writable({
  95. objectMode: true,
  96. write(entry, encoding, callback) {
  97. const gcpEntry = GCPManager.convertLogEntry(entry)
  98. // eslint-disable-next-line no-console
  99. console.log(JSON.stringify(gcpEntry, bunyan.safeCycles()))
  100. setImmediate(callback)
  101. },
  102. })
  103. return { level: this.defaultLevel, type: 'raw', stream }
  104. }
  105. case 'gce': {
  106. const { LoggingBunyan } = require('@google-cloud/logging-bunyan')
  107. return new LoggingBunyan({
  108. logName: this.loggerName,
  109. serviceContext: { service: this.loggerName },
  110. }).stream(this.defaultLevel)
  111. }
  112. default: {
  113. return { level: this.defaultLevel, stream: process.stdout }
  114. }
  115. }
  116. },
  117. _setupRingBuffer() {
  118. this.ringBufferSize = parseInt(process.env.LOG_RING_BUFFER_SIZE) || 0
  119. if (this.ringBufferSize > 0) {
  120. this.ringBuffer = new bunyan.RingBuffer({ limit: this.ringBufferSize })
  121. this.logger.addStream({
  122. level: 'trace',
  123. type: 'raw',
  124. stream: this.ringBuffer,
  125. })
  126. } else {
  127. this.ringBuffer = null
  128. }
  129. },
  130. _setupLogLevelChecker() {
  131. const logLevelSource = (
  132. process.env.LOG_LEVEL_SOURCE || 'file'
  133. ).toLowerCase()
  134. if (this.logLevelChecker) {
  135. this.logLevelChecker.stop()
  136. this.logLevelChecker = null
  137. }
  138. if (this.isProduction) {
  139. switch (logLevelSource) {
  140. case 'file':
  141. this.logLevelChecker = new FileLogLevelChecker(
  142. this.logger,
  143. this.defaultLevel
  144. )
  145. break
  146. case 'gce_metadata':
  147. this.logLevelChecker = new GCEMetadataLogLevelChecker(
  148. this.logger,
  149. this.defaultLevel
  150. )
  151. break
  152. case 'none':
  153. break
  154. default:
  155. // eslint-disable-next-line no-console
  156. console.log(`Unrecognised log level source: ${logLevelSource}`)
  157. }
  158. if (this.logLevelChecker) {
  159. this.logLevelChecker.start()
  160. }
  161. }
  162. },
  163. }
  164. LoggingManager.initialize('default')
  165. function handleWarning(err) {
  166. LoggingManager.warn({ err }, 'Warning details')
  167. }
  168. process.on('warning', handleWarning)
  169. LoggingManager.removeWarningHandler = () => {
  170. process.off('warning', handleWarning)
  171. }
  172. module.exports = LoggingManager