mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Fix RequestLogger object lifecycle
This commit is contained in:
parent
28fb998719
commit
035364b62e
2 changed files with 18 additions and 21 deletions
|
@ -16,8 +16,7 @@ const RequestLogger = require('./app/js/RequestLogger')
|
|||
|
||||
const app = express()
|
||||
|
||||
const requestLogger = new RequestLogger()
|
||||
requestLogger.attach(app)
|
||||
RequestLogger.attach(app)
|
||||
|
||||
if (settings.sentry && settings.sentry.dsn) {
|
||||
logger.initializeErrorReporting(settings.sentry.dsn)
|
||||
|
|
|
@ -3,25 +3,10 @@ const metrics = require('metrics-sharelatex')
|
|||
|
||||
class RequestLogger {
|
||||
constructor() {
|
||||
this.errorHandler = this.errorHandler.bind(this)
|
||||
this.middleware = this.middleware.bind(this)
|
||||
this._logInfo = {}
|
||||
this._logMessage = 'http request'
|
||||
}
|
||||
|
||||
attach(app) {
|
||||
app.use(this.middleware)
|
||||
app.use(this.errorHandler)
|
||||
}
|
||||
|
||||
errorHandler(err, req, res, next) {
|
||||
this._logInfo.error = err
|
||||
res
|
||||
.send(err.message)
|
||||
.status(500)
|
||||
.end()
|
||||
}
|
||||
|
||||
addFields(fields) {
|
||||
Object.assign(this._logInfo, fields)
|
||||
}
|
||||
|
@ -30,9 +15,22 @@ class RequestLogger {
|
|||
this._logMessage = message
|
||||
}
|
||||
|
||||
middleware(req, res, next) {
|
||||
static attach(app) {
|
||||
app.use(RequestLogger.middleware)
|
||||
app.use(RequestLogger.errorHandler)
|
||||
}
|
||||
|
||||
static errorHandler(err, req, res, next) {
|
||||
this._logInfo.error = err
|
||||
res
|
||||
.send(err.message)
|
||||
.status(500)
|
||||
.end()
|
||||
}
|
||||
|
||||
static middleware(req, res, next) {
|
||||
const startTime = new Date()
|
||||
req.requestLogger = this
|
||||
req.requestLogger = new RequestLogger()
|
||||
|
||||
// override the 'end' method to log and record metrics
|
||||
const end = res.end
|
||||
|
@ -77,9 +75,9 @@ class RequestLogger {
|
|||
statusCode: res.statusCode,
|
||||
'response-time': responseTime
|
||||
},
|
||||
info: this._logInfo
|
||||
info: req.requestLogger._logInfo
|
||||
},
|
||||
this._logMessage
|
||||
req.requestLogger._logMessage
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue