2021-10-27 12:06:50 +00:00
|
|
|
const Stream = require('stream')
|
2019-03-08 16:06:24 +00:00
|
|
|
const bunyan = require('bunyan')
|
2019-10-24 20:41:46 +00:00
|
|
|
const yn = require('yn')
|
2021-10-27 12:06:50 +00:00
|
|
|
const GCPManager = require('./gcp-manager')
|
|
|
|
const SentryManager = require('./sentry-manager')
|
|
|
|
const Serializers = require('./serializers')
|
|
|
|
const {
|
|
|
|
FileLogLevelChecker,
|
|
|
|
GCEMetadataLogLevelChecker
|
|
|
|
} = require('./log-level-checker')
|
|
|
|
|
|
|
|
const LoggingManager = {
|
2019-03-08 14:09:44 +00:00
|
|
|
initialize(name) {
|
|
|
|
this.isProduction =
|
2020-07-02 09:51:37 +00:00
|
|
|
(process.env.NODE_ENV || '').toLowerCase() === 'production'
|
2019-03-08 14:09:44 +00:00
|
|
|
this.defaultLevel =
|
2020-07-02 09:51:37 +00:00
|
|
|
process.env.LOG_LEVEL || (this.isProduction ? 'warn' : 'debug')
|
2019-03-08 16:06:24 +00:00
|
|
|
this.loggerName = name
|
2019-03-08 14:09:44 +00:00
|
|
|
this.logger = bunyan.createLogger({
|
|
|
|
name,
|
2019-07-11 10:11:09 +00:00
|
|
|
serializers: {
|
2021-10-27 12:06:50 +00:00
|
|
|
err: Serializers.err,
|
|
|
|
req: Serializers.req,
|
|
|
|
res: Serializers.res
|
2019-07-11 10:11:09 +00:00
|
|
|
},
|
2021-10-27 12:06:50 +00:00
|
|
|
streams: [this._getOutputStreamConfig()]
|
2019-03-08 16:06:24 +00:00
|
|
|
})
|
2019-10-24 20:41:46 +00:00
|
|
|
this._setupRingBuffer()
|
|
|
|
this._setupLogLevelChecker()
|
2019-03-08 16:06:24 +00:00
|
|
|
return this
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
2021-09-22 10:25:28 +00:00
|
|
|
initializeErrorReporting(dsn, options) {
|
2021-10-27 12:06:50 +00:00
|
|
|
this.sentryManager = new SentryManager()
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
debug() {
|
2019-03-08 16:06:24 +00:00
|
|
|
return this.logger.debug.apply(this.logger, arguments)
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
info() {
|
2019-03-08 16:06:24 +00:00
|
|
|
return this.logger.info.apply(this.logger, arguments)
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
log() {
|
2019-03-08 16:06:24 +00:00
|
|
|
return this.logger.info.apply(this.logger, arguments)
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
error(attributes, message, ...args) {
|
2019-03-12 12:23:37 +00:00
|
|
|
if (this.ringBuffer !== null && Array.isArray(this.ringBuffer.records)) {
|
2020-07-02 10:01:23 +00:00
|
|
|
attributes.logBuffer = this.ringBuffer.records.filter(function (record) {
|
2019-03-12 12:23:37 +00:00
|
|
|
return record.level !== 50
|
|
|
|
})
|
2019-03-08 14:09:44 +00:00
|
|
|
}
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.error(attributes, message, ...Array.from(args))
|
2021-10-27 12:06:50 +00:00
|
|
|
if (this.sentryManager) {
|
|
|
|
this.sentryManager.captureExceptionRateLimited(attributes, message)
|
2019-03-08 14:09:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
err() {
|
2019-03-08 16:06:24 +00:00
|
|
|
return this.error.apply(this, arguments)
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
warn() {
|
2019-03-08 16:06:24 +00:00
|
|
|
return this.logger.warn.apply(this.logger, arguments)
|
2019-03-08 14:09:44 +00:00
|
|
|
},
|
|
|
|
|
2021-09-22 10:25:28 +00:00
|
|
|
fatal(attributes, message) {
|
2019-03-08 16:06:24 +00:00
|
|
|
this.logger.fatal(attributes, message)
|
2021-10-27 12:06:50 +00:00
|
|
|
if (this.sentryManager) {
|
|
|
|
this.sentryManager.captureException(attributes, message, 'fatal')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getOutputStreamConfig() {
|
|
|
|
const gcpEnabled = yn(process.env.GCP_LOGGING)
|
|
|
|
if (gcpEnabled) {
|
|
|
|
const stream = new Stream.Writable({
|
|
|
|
objectMode: true,
|
|
|
|
write(entry, encoding, callback) {
|
|
|
|
const gcpEntry = GCPManager.convertLogEntry(entry)
|
|
|
|
console.log(JSON.stringify(gcpEntry, bunyan.safeCycles()))
|
|
|
|
setImmediate(callback)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return { level: this.defaultLevel, type: 'raw', stream }
|
|
|
|
} else {
|
|
|
|
return { level: this.defaultLevel, stream: process.stdout }
|
2019-03-08 14:09:44 +00:00
|
|
|
}
|
2019-10-24 20:41:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_setupRingBuffer() {
|
2020-07-02 09:51:37 +00:00
|
|
|
this.ringBufferSize = parseInt(process.env.LOG_RING_BUFFER_SIZE) || 0
|
2019-10-24 20:41:46 +00:00
|
|
|
if (this.ringBufferSize > 0) {
|
|
|
|
this.ringBuffer = new bunyan.RingBuffer({ limit: this.ringBufferSize })
|
|
|
|
this.logger.addStream({
|
|
|
|
level: 'trace',
|
|
|
|
type: 'raw',
|
|
|
|
stream: this.ringBuffer
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.ringBuffer = null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-10-27 12:06:50 +00:00
|
|
|
_setupLogLevelChecker() {
|
|
|
|
const logLevelSource = (
|
|
|
|
process.env.LOG_LEVEL_SOURCE || 'file'
|
|
|
|
).toLowerCase()
|
|
|
|
|
|
|
|
if (this.logLevelChecker) {
|
|
|
|
this.logLevelChecker.stop()
|
|
|
|
this.logLevelChecker = null
|
2019-10-24 20:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isProduction) {
|
2021-10-27 12:06:50 +00:00
|
|
|
switch (logLevelSource) {
|
|
|
|
case 'file':
|
|
|
|
this.logLevelChecker = new FileLogLevelChecker(this.logger)
|
|
|
|
break
|
|
|
|
case 'gce_metadata':
|
|
|
|
this.logLevelChecker = new GCEMetadataLogLevelChecker(this.logger)
|
|
|
|
break
|
|
|
|
case 'none':
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
console.log(`Unrecognised log level source: ${logLevelSource}`)
|
2019-10-24 20:41:46 +00:00
|
|
|
}
|
2021-10-27 12:06:50 +00:00
|
|
|
if (this.logLevelChecker) {
|
|
|
|
this.logLevelChecker.start()
|
2020-07-24 10:59:23 +00:00
|
|
|
}
|
2019-10-24 20:41:46 +00:00
|
|
|
}
|
2019-03-08 14:09:44 +00:00
|
|
|
}
|
2021-10-27 12:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoggingManager.initialize('default-sharelatex')
|
2019-03-08 14:09:44 +00:00
|
|
|
|
2021-10-27 12:06:50 +00:00
|
|
|
module.exports = LoggingManager
|