overleaf/libraries/logger/log-level-checker.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

60 lines
1.5 KiB
JavaScript

const { fetchString } = require('@overleaf/fetch-utils')
const fs = require('node: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)
this.checkInterval.unref()
}
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 (e) {
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 strEndTime = await fetchString(uri, options)
return parseInt(strEndTime, 10)
}
}
module.exports = { FileLogLevelChecker, GCEMetadataLogLevelChecker }