2019-03-08 11:06:24 -05:00
|
|
|
const bunyan = require('bunyan')
|
2020-07-06 09:53:49 -04:00
|
|
|
const fetch = require('node-fetch')
|
2020-05-15 07:31:28 -04:00
|
|
|
const fs = require('fs')
|
2019-10-24 16:41:46 -04:00
|
|
|
const yn = require('yn')
|
2019-07-11 06:11:09 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-10-24 16:41:46 -04:00
|
|
|
const GCPLogging = require('@google-cloud/logging-bunyan')
|
2019-07-11 06:11:09 -04:00
|
|
|
|
|
|
|
// bunyan error serializer
|
2020-07-02 06:01:23 -04:00
|
|
|
const errSerializer = function (err) {
|
2019-10-23 16:25:48 -04:00
|
|
|
if (!err || !err.stack) {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-11 06:11:09 -04:00
|
|
|
return {
|
|
|
|
message: err.message,
|
|
|
|
name: err.name,
|
|
|
|
stack: OError.getFullStack(err),
|
|
|
|
info: OError.getFullInfo(err),
|
|
|
|
code: err.code,
|
|
|
|
signal: err.signal
|
2019-10-23 16:25:48 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 09:09:44 -05:00
|
|
|
|
2019-10-23 16:25:48 -04:00
|
|
|
const Logger = (module.exports = {
|
2019-03-08 09:09:44 -05:00
|
|
|
initialize(name) {
|
2020-07-24 06:59:23 -04:00
|
|
|
this.logLevelSource = (process.env.LOG_LEVEL_SOURCE || 'file').toLowerCase()
|
2019-03-08 09:09:44 -05:00
|
|
|
this.isProduction =
|
2020-07-02 05:51:37 -04:00
|
|
|
(process.env.NODE_ENV || '').toLowerCase() === 'production'
|
2019-03-08 09:09:44 -05:00
|
|
|
this.defaultLevel =
|
2020-07-02 05:51:37 -04:00
|
|
|
process.env.LOG_LEVEL || (this.isProduction ? 'warn' : 'debug')
|
2019-03-08 11:06:24 -05:00
|
|
|
this.loggerName = name
|
2019-03-08 09:09:44 -05:00
|
|
|
this.logger = bunyan.createLogger({
|
|
|
|
name,
|
2019-07-11 06:11:09 -04:00
|
|
|
serializers: {
|
|
|
|
err: errSerializer,
|
|
|
|
req: bunyan.stdSerializers.req,
|
|
|
|
res: bunyan.stdSerializers.res
|
|
|
|
},
|
2019-10-24 16:41:46 -04:00
|
|
|
streams: [{ level: this.defaultLevel, stream: process.stdout }]
|
2019-03-08 11:06:24 -05:00
|
|
|
})
|
2019-10-24 16:41:46 -04:00
|
|
|
this._setupRingBuffer()
|
|
|
|
this._setupStackdriver()
|
|
|
|
this._setupLogLevelChecker()
|
2019-03-08 11:06:24 -05:00
|
|
|
return this
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
2020-07-30 07:49:54 -04:00
|
|
|
async checkLogLevel() {
|
2020-07-20 11:57:43 -04:00
|
|
|
try {
|
2020-07-30 07:49:54 -04:00
|
|
|
const end = await this.getTracingEndTime()
|
|
|
|
if (parseInt(end, 10) > Date.now()) {
|
2019-03-08 11:06:24 -05:00
|
|
|
this.logger.level('trace')
|
2019-03-08 09:09:44 -05:00
|
|
|
} else {
|
2019-03-08 11:06:24 -05:00
|
|
|
this.logger.level(this.defaultLevel)
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2020-07-20 11:57:43 -04:00
|
|
|
} catch (err) {
|
|
|
|
this.logger.level(this.defaultLevel)
|
|
|
|
}
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
2020-07-30 07:49:54 -04:00
|
|
|
async getTracingEndTimeFile() {
|
2020-07-30 10:08:22 -04:00
|
|
|
return fs.promises.readFile('/logging/tracingEndTime')
|
2020-07-30 07:49:54 -04:00
|
|
|
},
|
|
|
|
|
2020-07-30 10:08:22 -04:00
|
|
|
async getTracingEndTimeMetadata() {
|
2020-07-03 02:17:49 -04:00
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
'Metadata-Flavor': 'Google'
|
|
|
|
}
|
2020-07-06 09:53:49 -04:00
|
|
|
}
|
|
|
|
const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.loggerName}-setLogLevelEndTime`
|
2020-07-30 10:08:22 -04:00
|
|
|
const res = await fetch(uri, options)
|
|
|
|
if (!res.ok) throw new Error('Metadata not okay')
|
2020-07-30 07:49:54 -04:00
|
|
|
return res.text()
|
2020-07-03 02:17:49 -04:00
|
|
|
},
|
|
|
|
|
2019-10-23 16:25:48 -04:00
|
|
|
initializeErrorReporting(sentryDsn, options) {
|
2019-03-08 11:06:24 -05:00
|
|
|
const raven = require('raven')
|
2019-10-23 16:25:48 -04:00
|
|
|
this.raven = new raven.Client(sentryDsn, options)
|
2019-03-08 11:06:24 -05:00
|
|
|
this.lastErrorTimeStamp = 0 // for rate limiting on sentry reporting
|
|
|
|
this.lastErrorCount = 0
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
captureException(attributes, message, level) {
|
|
|
|
// handle case of logger.error "message"
|
2019-03-08 11:06:24 -05:00
|
|
|
let key, value
|
|
|
|
if (typeof attributes === 'string') {
|
|
|
|
attributes = { err: new Error(attributes) }
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
// extract any error object
|
2019-03-08 11:06:24 -05:00
|
|
|
let error = attributes.err || attributes.error
|
2019-03-08 09:09:44 -05:00
|
|
|
// avoid reporting errors twice
|
|
|
|
for (key in attributes) {
|
2019-03-08 11:06:24 -05:00
|
|
|
value = attributes[key]
|
2019-03-08 09:09:44 -05:00
|
|
|
if (value instanceof Error && value.reportedToSentry) {
|
2019-03-08 11:06:24 -05:00
|
|
|
return
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// include our log message in the error report
|
|
|
|
if (error == null) {
|
2019-03-08 11:06:24 -05:00
|
|
|
if (typeof message === 'string') {
|
|
|
|
error = { message }
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
} else if (message != null) {
|
2019-03-08 11:06:24 -05:00
|
|
|
attributes.description = message
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
// report the error
|
|
|
|
if (error != null) {
|
|
|
|
// capture attributes and use *_id objects as tags
|
2019-03-08 11:06:24 -05:00
|
|
|
const tags = {}
|
|
|
|
const extra = {}
|
2019-03-08 09:09:44 -05:00
|
|
|
for (key in attributes) {
|
2019-03-08 11:06:24 -05:00
|
|
|
value = attributes[key]
|
|
|
|
if (key.match(/_id/) && typeof value === 'string') {
|
|
|
|
tags[key] = value
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-03-08 11:06:24 -05:00
|
|
|
extra[key] = value
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
// capture req object if available
|
2019-03-08 11:06:24 -05:00
|
|
|
const { req } = attributes
|
2019-03-08 09:09:44 -05:00
|
|
|
if (req != null) {
|
|
|
|
extra.req = {
|
|
|
|
method: req.method,
|
|
|
|
url: req.originalUrl,
|
|
|
|
query: req.query,
|
|
|
|
headers: req.headers,
|
|
|
|
ip: req.ip
|
2019-03-08 11:06:24 -05:00
|
|
|
}
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
// recreate error objects that have been converted to a normal object
|
2019-03-08 11:06:24 -05:00
|
|
|
if (!(error instanceof Error) && typeof error === 'object') {
|
|
|
|
const newError = new Error(error.message)
|
2019-03-08 09:09:44 -05:00
|
|
|
for (key of Object.keys(error || {})) {
|
2019-03-08 11:06:24 -05:00
|
|
|
value = error[key]
|
|
|
|
newError[key] = value
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-03-08 11:06:24 -05:00
|
|
|
error = newError
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
// filter paths from the message to avoid duplicate errors in sentry
|
|
|
|
// (e.g. errors from `fs` methods which have a path attribute)
|
|
|
|
try {
|
|
|
|
if (error.path) {
|
2019-03-08 11:06:24 -05:00
|
|
|
error.message = error.message.replace(` '${error.path}'`, '')
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-10-23 16:25:48 -04:00
|
|
|
|
|
|
|
// send the error to sentry
|
2019-03-08 11:06:24 -05:00
|
|
|
this.raven.captureException(error, { tags, extra, level })
|
2019-10-23 16:25:48 -04:00
|
|
|
|
2019-03-08 09:09:44 -05:00
|
|
|
// put a flag on the errors to avoid reporting them multiple times
|
2019-10-23 16:25:48 -04:00
|
|
|
for (key in attributes) {
|
|
|
|
value = attributes[key]
|
|
|
|
if (value instanceof Error) {
|
|
|
|
value.reportedToSentry = true
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-10-23 16:25:48 -04:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// ignore Raven errors
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
debug() {
|
2019-03-08 11:06:24 -05:00
|
|
|
return this.logger.debug.apply(this.logger, arguments)
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
info() {
|
2019-03-08 11:06:24 -05:00
|
|
|
return this.logger.info.apply(this.logger, arguments)
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
log() {
|
2019-03-08 11:06:24 -05:00
|
|
|
return this.logger.info.apply(this.logger, arguments)
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
error(attributes, message, ...args) {
|
2019-03-12 08:23:37 -04:00
|
|
|
if (this.ringBuffer !== null && Array.isArray(this.ringBuffer.records)) {
|
2020-07-02 06:01:23 -04:00
|
|
|
attributes.logBuffer = this.ringBuffer.records.filter(function (record) {
|
2019-03-12 08:23:37 -04:00
|
|
|
return record.level !== 50
|
|
|
|
})
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-03-08 11:06:24 -05:00
|
|
|
this.logger.error(attributes, message, ...Array.from(args))
|
2019-03-08 09:09:44 -05:00
|
|
|
if (this.raven != null) {
|
2019-03-08 11:06:24 -05:00
|
|
|
const MAX_ERRORS = 5 // maximum number of errors in 1 minute
|
|
|
|
const now = new Date()
|
2019-03-08 09:09:44 -05:00
|
|
|
// have we recently reported an error?
|
2019-03-08 11:06:24 -05:00
|
|
|
const recentSentryReport = now - this.lastErrorTimeStamp < 60 * 1000
|
2019-03-08 09:09:44 -05:00
|
|
|
// if so, increment the error count
|
|
|
|
if (recentSentryReport) {
|
2019-03-08 11:06:24 -05:00
|
|
|
this.lastErrorCount++
|
2019-03-08 09:09:44 -05:00
|
|
|
} else {
|
2019-03-08 11:06:24 -05:00
|
|
|
this.lastErrorCount = 0
|
|
|
|
this.lastErrorTimeStamp = now
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
// only report 5 errors every minute to avoid overload
|
|
|
|
if (this.lastErrorCount < MAX_ERRORS) {
|
|
|
|
// add a note if the rate limit has been hit
|
|
|
|
const note =
|
2019-03-08 11:06:24 -05:00
|
|
|
this.lastErrorCount + 1 === MAX_ERRORS ? '(rate limited)' : ''
|
2019-03-08 09:09:44 -05:00
|
|
|
// report the exception
|
2019-03-08 11:06:24 -05:00
|
|
|
return this.captureException(attributes, message, `error${note}`)
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
err() {
|
2019-03-08 11:06:24 -05:00
|
|
|
return this.error.apply(this, arguments)
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
warn() {
|
2019-03-08 11:06:24 -05:00
|
|
|
return this.logger.warn.apply(this.logger, arguments)
|
2019-03-08 09:09:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
fatal(attributes, message, callback) {
|
|
|
|
if (callback == null) {
|
2020-07-02 06:01:23 -04:00
|
|
|
callback = function () {}
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-03-08 11:06:24 -05:00
|
|
|
this.logger.fatal(attributes, message)
|
2019-03-08 09:09:44 -05:00
|
|
|
if (this.raven != null) {
|
2020-07-02 06:01:23 -04:00
|
|
|
var cb = function (e) {
|
2019-03-08 09:09:44 -05:00
|
|
|
// call the callback once after 'logged' or 'error' event
|
2019-03-08 11:06:24 -05:00
|
|
|
callback()
|
2020-07-02 06:01:23 -04:00
|
|
|
return (cb = function () {})
|
2019-03-08 11:06:24 -05:00
|
|
|
}
|
|
|
|
this.captureException(attributes, message, 'fatal')
|
|
|
|
this.raven.once('logged', cb)
|
|
|
|
return this.raven.once('error', cb)
|
2019-03-08 09:09:44 -05:00
|
|
|
} else {
|
2019-03-08 11:06:24 -05:00
|
|
|
return callback()
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-10-24 16:41:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_setupRingBuffer() {
|
2020-07-02 05:51:37 -04:00
|
|
|
this.ringBufferSize = parseInt(process.env.LOG_RING_BUFFER_SIZE) || 0
|
2019-10-24 16:41:46 -04: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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setupStackdriver() {
|
2020-07-02 05:51:37 -04:00
|
|
|
const stackdriverEnabled = yn(process.env.STACKDRIVER_LOGGING)
|
2019-10-24 16:41:46 -04:00
|
|
|
if (!stackdriverEnabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const stackdriverClient = new GCPLogging.LoggingBunyan({
|
|
|
|
logName: this.loggerName,
|
|
|
|
serviceContext: { service: this.loggerName }
|
|
|
|
})
|
|
|
|
this.logger.addStream(stackdriverClient.stream(this.defaultLevel))
|
|
|
|
},
|
|
|
|
|
|
|
|
_setupLogLevelChecker() {
|
|
|
|
if (this.isProduction) {
|
|
|
|
// clear interval if already set
|
|
|
|
if (this.checkInterval) {
|
|
|
|
clearInterval(this.checkInterval)
|
|
|
|
}
|
2020-07-24 06:59:23 -04:00
|
|
|
if (this.logLevelSource === 'file') {
|
2020-07-30 07:49:54 -04:00
|
|
|
this.getTracingEndTime = this.getTracingEndTimeFile
|
2020-07-24 06:59:23 -04:00
|
|
|
} else if (this.logLevelSource === 'gce_metadata') {
|
2020-07-30 10:08:22 -04:00
|
|
|
this.getTracingEndTime = this.getTracingEndTimeMetadata
|
2020-07-30 07:49:54 -04:00
|
|
|
} else if (this.logLevelSource === 'none') {
|
|
|
|
return
|
|
|
|
} else {
|
2020-07-24 06:59:23 -04:00
|
|
|
console.log('Unrecognised log level source')
|
2020-07-30 07:49:54 -04:00
|
|
|
return
|
2020-07-24 06:59:23 -04:00
|
|
|
}
|
2020-07-30 07:49:54 -04:00
|
|
|
// check for log level override on startup
|
2020-08-11 05:48:44 -04:00
|
|
|
this.checkLogLevel()
|
2020-07-30 07:49:54 -04:00
|
|
|
// re-check log level every minute
|
|
|
|
this.checkInterval = setInterval(this.checkLogLevel.bind(this), 1000 * 60)
|
2019-10-24 16:41:46 -04:00
|
|
|
}
|
2019-03-08 09:09:44 -05:00
|
|
|
}
|
2019-10-23 16:25:48 -04:00
|
|
|
})
|
2019-03-08 09:09:44 -05:00
|
|
|
|
2019-03-08 11:06:24 -05:00
|
|
|
Logger.initialize('default-sharelatex')
|