2023-09-07 07:57:11 -04:00
|
|
|
const { fetchString } = require('@overleaf/fetch-utils')
|
2021-10-27 08:06:50 -04:00
|
|
|
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)
|
2024-06-17 04:10:04 -04:00
|
|
|
this.checkInterval.unref()
|
2021-10-27 08:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-12-16 04:04:32 -05:00
|
|
|
} catch (e) {
|
2021-10-27 08:06:50 -04:00
|
|
|
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: {
|
2021-12-16 04:04:32 -05:00
|
|
|
'Metadata-Flavor': 'Google',
|
|
|
|
},
|
2021-10-27 08:06:50 -04:00
|
|
|
}
|
|
|
|
const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.logger.fields.name}-setLogLevelEndTime`
|
2023-09-07 07:57:11 -04:00
|
|
|
const strEndTime = await fetchString(uri, options)
|
2021-10-27 08:06:50 -04:00
|
|
|
return parseInt(strEndTime, 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { FileLogLevelChecker, GCEMetadataLogLevelChecker }
|