Implement logger and bar options

This commit is contained in:
2023-11-27 11:30:49 +01:00
parent 74a4204046
commit a0b4e51695
5 changed files with 84 additions and 54 deletions

View File

@@ -7,17 +7,69 @@ const defaultFormat = printf(({ level, message, timestamp }) => {
const sessionFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [Session ${label}] ${level}: ${message}`;
});
function createBaseLogger() {
return createLogger({
function createBaseLogger(options) {
const logger = createLogger({
format: combine(format.colorize({ all: true }), timestamp(), defaultFormat),
transports: [new transports.Console()],
});
const oldInfo = logger.info;
const oldWarn = logger.info;
const oldError = logger.error;
logger.info = function (input) {
if (!shouldLog(options)) {
return;
}
oldInfo(input);
};
logger.error = function (input) {
if (!shouldLog(options)) {
return;
}
oldError(input);
};
logger.warn = function (input) {
if (!shouldLog(options)) {
return;
}
oldWarn(input);
};
return logger;
}
function createSessionLogger(ilabel) {
return createLogger({
function createSessionLogger(options, ilabel) {
const logger = createLogger({
format: combine(label({ label: ilabel }), format.colorize({ all: true }), timestamp(), sessionFormat),
transports: [new transports.Console()],
});
const oldInfo = logger.info;
const oldWarn = logger.info;
const oldError = logger.error;
logger.info = function (input) {
if (!shouldLog(options)) {
return;
}
oldInfo(input);
};
logger.error = function (input) {
if (!shouldLog(options)) {
return;
}
oldError(input);
};
logger.warn = function (input) {
if (!shouldLog(options)) {
return;
}
oldWarn(input);
};
return logger;
}
function shouldLog(options) {
return options.logs || !options.bars;
}
module.exports = { createBaseLogger, createSessionLogger };