All files / src/utils logger.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 5/5
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57      31x   31x   31x 8x   31x 8x     31x     44x     31x         31x           155x 44x 9x 9x 9x   35x       31x 31x 31x 31x 31x                      
import winston from 'winston';
import type { ProgressBar } from '../lib/progress/index.js';
 
const { format } = winston;
 
let progressBar: ProgressBar | null = null;
 
const setProgressBar = (bar: ProgressBar) => {
  progressBar = bar;
};
const removeProgressBar = () => {
  progressBar = null;
};
 
const consoleFormat = format.combine(
  format.colorize(),
  format.errors({ stack: true }),
  format.printf(info => `${info.level}: ${info.stack || info.message}`),
);
 
const consoleTransport = new winston.transports.Console({
  handleExceptions: true,
  level: 'debug',
});
 
winston.configure({
  exitOnError: false,
  format: consoleFormat,
  transports: [consoleTransport],
});
 
const createLoggerThatInterruptsProgressBar = (level: string) => (...args: any[]) => {
  if (progressBar) {
    progressBar.interruptBegin();
    (winston as any).log(level, ...args);
    progressBar.interruptEnd();
  } else {
    (winston as any).log(level, ...args);
  }
};
 
const errorWrap = createLoggerThatInterruptsProgressBar('error');
const warnWrap = createLoggerThatInterruptsProgressBar('warn');
const infoWrap = createLoggerThatInterruptsProgressBar('info');
const verboseWrap = createLoggerThatInterruptsProgressBar('verbose');
const debugWrap = createLoggerThatInterruptsProgressBar('debug');
 
export {
  errorWrap as error,
  warnWrap as warn,
  infoWrap as info,
  verboseWrap as verbose,
  debugWrap as debug,
  setProgressBar,
  removeProgressBar,
};