From 624f50dad85f3f5ac4cdc0e975d4286edcde6562 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Mon, 12 Jun 2023 19:24:36 +0200 Subject: [PATCH] Fix the log to file issue. (#3879) This bug happened when we moved to ESM and there's no new release for Intel with a fix (yet). https://github.com/sitespeedio/sitespeed.io/issues/3878 --- .github/workflows/linux.yml | 2 +- lib/core/logging.js | 45 ++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 285e87bb9..76d8972ce 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -65,6 +65,6 @@ jobs: - name: Run test without a CLI run: xvfb-run node test/runWithoutCli.js - name: Run test with Influx 1.8 - run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --xvfb + run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --xvfb --logToFile - name: Run test with Influx 2.6.1 run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --influxdb.port 8087 --influxdb.version 2 --influxdb.organisation sitespeed --influxdb.token sitespeed --xvfb \ No newline at end of file diff --git a/lib/core/logging.js b/lib/core/logging.js index 2439dfb28..51a284534 100644 --- a/lib/core/logging.js +++ b/lib/core/logging.js @@ -1,5 +1,6 @@ import intel from 'intel'; - +import { createWriteStream } from 'node:fs'; +import { inherits } from 'node:util'; const { INFO, DEBUG, @@ -7,11 +8,44 @@ const { TRACE, NONE, basicConfig, - addHandler, - handlers, + Logger, + Handler, Formatter } = intel; +// FileHandler isn't exposed in Intel when we moved to ESM. +// To fix that for now we just use the same code as Intel. + +function StreamHandler(options) { + options = options || {}; + if (!options.stream) { + options = { stream: options }; + } + Handler.call(this, options); + this._stream = options.stream; +} + +inherits(StreamHandler, Handler); + +StreamHandler.prototype.emit = function streamEmit(record) { + this._stream.write(this.format(record) + '\n'); +}; + +function FileHandler(options) { + if (typeof options === 'string') { + options = { file: options }; + } + this._file = options.file; + + options.stream = this._open(); + StreamHandler.call(this, options); +} +inherits(FileHandler, StreamHandler); + +FileHandler.prototype._open = function open() { + return createWriteStream(this._file, { flags: 'a' }); +}; + export function configure(options, logDir) { options = options || {}; @@ -52,8 +86,9 @@ export function configure(options, logDir) { } if (options.logToFile) { - addHandler( - new handlers.File({ + let logger = new Logger(); + logger.addHandler( + new FileHandler({ file: logDir + '/sitespeed.io.log', formatter: new Formatter({ format: '[%(date)s] %(levelname)s: [%(name)s] %(message)s',