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
This commit is contained in:
Peter Hedenskog 2023-06-12 19:24:36 +02:00 committed by GitHub
parent 73a92fa8a0
commit 624f50dad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 6 deletions

View File

@ -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

View File

@ -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',