mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-27 05:13:49 +00:00
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
|
const fetch = require('node-fetch')
|
||
|
const fs = require('fs')
|
||
|
|
||
|
class LogLevelChecker {
|
||
|
constructor(logger, defaultLevel) {
|
||
|
this.logger = logger
|
||
|
this.defaultLevel = defaultLevel
|
||
|
}
|
||
|
|
||
|
start() {
|
||
|
// check for log level override on startup
|
||
|
this.checkLogLevel()
|
||
|
// re-check log level every minute
|
||
|
this.checkInterval = setInterval(this.checkLogLevel.bind(this), 1000 * 60)
|
||
|
}
|
||
|
|
||
|
stop() {
|
||
|
clearInterval(this.checkInterval)
|
||
|
}
|
||
|
|
||
|
async checkLogLevel() {
|
||
|
try {
|
||
|
const end = await this.getTracingEndTime()
|
||
|
if (end > Date.now()) {
|
||
|
this.logger.level('trace')
|
||
|
} else {
|
||
|
this.logger.level(this.defaultLevel)
|
||
|
}
|
||
|
} catch {
|
||
|
this.logger.level(this.defaultLevel)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async getTracingEndTime() {
|
||
|
return 0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class FileLogLevelChecker extends LogLevelChecker {
|
||
|
async getTracingEndTime() {
|
||
|
const strEndTime = await fs.promises.readFile('/logging/tracingEndTime')
|
||
|
return parseInt(strEndTime, 10)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class GCEMetadataLogLevelChecker extends LogLevelChecker {
|
||
|
async getTracingEndTime() {
|
||
|
const options = {
|
||
|
headers: {
|
||
|
'Metadata-Flavor': 'Google'
|
||
|
}
|
||
|
}
|
||
|
const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.logger.fields.name}-setLogLevelEndTime`
|
||
|
const res = await fetch(uri, options)
|
||
|
if (!res.ok) {
|
||
|
throw new Error('Metadata not okay')
|
||
|
}
|
||
|
const strEndTime = await res.text()
|
||
|
return parseInt(strEndTime, 10)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = { FileLogLevelChecker, GCEMetadataLogLevelChecker }
|