actual-moneymoney
Version:
An importer for syncing MoneyMoney accounts and transactions to Actual.
62 lines (61 loc) • 2.03 kB
JavaScript
export var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
LogLevel[LogLevel["WARN"] = 1] = "WARN";
LogLevel[LogLevel["INFO"] = 2] = "INFO";
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
LogLevel[LogLevel["ACTUAL"] = 4] = "ACTUAL";
})(LogLevel || (LogLevel = {}));
const colorizeText = (color, text) => {
const codes = {
red: '\x1b[31m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
gray: '\x1b[90m',
magenta: '\x1b[35m',
};
return `${codes[color]}${text}\x1b[0m`;
};
class Logger {
constructor(logLevel = LogLevel.INFO) {
this.logLevel = LogLevel.INFO;
// We use a private reference to console.log to allow suppressing logs in other parts of the code
this.consoleLog = console.log;
this.logLevel = logLevel;
}
error(message, hint) {
this.log(LogLevel.ERROR, message, hint);
}
warn(message, hint) {
this.log(LogLevel.WARN, message, hint);
}
info(message, hint) {
this.log(LogLevel.INFO, message, hint);
}
debug(message, hint) {
this.log(LogLevel.DEBUG, message, hint);
}
actual(message, hint) {
this.log(LogLevel.ACTUAL, message, hint);
}
log(level, message, hint) {
if (this.logLevel >= level) {
const prefix = `[${LogLevel[level].toUpperCase()}]`;
const color = {
[LogLevel.ERROR]: 'red',
[LogLevel.WARN]: 'yellow',
[LogLevel.INFO]: 'cyan',
[LogLevel.DEBUG]: 'gray',
[LogLevel.ACTUAL]: 'magenta',
}[level];
this.consoleLog(colorizeText(color, prefix), message);
if (hint) {
const arrayHint = Array.isArray(hint) ? hint : [hint];
for (const hint of arrayHint) {
this.consoleLog(colorizeText('gray', `${' '.repeat(prefix.length)} ↳ ${hint}`));
}
}
}
}
}
export default Logger;