baldrick-broth
Version:
Build automation tool and task runner
75 lines (74 loc) • 2.17 kB
JavaScript
import { readFile } from 'node:fs/promises';
import winston from 'winston';
import { coloration } from './coloration.js';
import { isCI } from './is-ci.js';
const { printf } = winston.format;
const consoleLikeFormat = printf(({ message }) => {
return message;
});
class BrothLogger {
thatLogger;
constructor() {
this.thatLogger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.File({
filename: `temp/log/baldrick-broth-log.txt`,
options: { flags: 'a' },
format: consoleLikeFormat,
}),
],
});
}
info(message) {
this.thatLogger.info(message);
}
warn(message) {
this.thatLogger.warn(message);
}
error(message) {
this.thatLogger.error(message);
}
}
/**
* Warning: despite expectation the currentTaskLogger is instanciated multiple times,
* possibly because the file is imported multiple times.
*/
export const currentTaskLogger = new BrothLogger();
export const currentTaskWarn = (content) => {
if (isCI) {
currentTaskLogger.warn(`✗ ${content.message}`);
}
else {
currentTaskLogger.warn(coloration.warn('✗') + ' ' + content.coloredMessage);
}
};
export const telemetryTaskLogger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.File({
filename: 'temp/log/baldrick-broth-telemetry.csv',
format: consoleLikeFormat,
}),
],
});
export const telemetryTaskRefLogger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.File({
filename: 'temp/log/baldrick-broth-telemetry-ref.csv',
format: consoleLikeFormat,
}),
],
});
export const replayLogToConsole = async () => {
try {
const currentLog = await readFile('temp/log/baldrick-broth-log.txt', {
encoding: 'utf8',
});
console.log(['', '', currentLog].join('\n'));
}
catch (error) {
console.error('Could not replay the log', error);
}
};