UNPKG

@aspecto/logger

Version:

_How to install_

69 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("./types"); const COLORS = { cyan: '\x1b[36m', magenta: '\x1b[35m', blue: '\x1b[34m', green: '\x1b[32m', yellow: '\x1b[33m', red: '\x1b[31m', dim: '\x1b[2m', reset: '\x1b[0m', bold: '\x1b[1m', }; const getExtra = (extra) => { if (extra instanceof Error) { return [COLORS.red + extra.stack + COLORS.reset]; } const logs = []; Object.entries(extra).forEach(([key, value]) => { if (value instanceof Error) { logs.push(COLORS.dim + COLORS.bold + ` ${key}: ` + COLORS.reset + COLORS.red + value.stack + COLORS.reset); } else logs.push(COLORS.dim + COLORS.bold + ` ${key}: ` + COLORS.reset + COLORS.dim + `${JSON.stringify(value, null, 4)}` + COLORS.reset); }); return logs; }; const developmentLogger = (level, _, message, extra) => { let color; switch (level) { case types_1.LogLevel.Trace: color = COLORS.magenta; break; case types_1.LogLevel.Debug: color = COLORS.blue; break; case types_1.LogLevel.Info: color = COLORS.green; break; case types_1.LogLevel.Warning: color = COLORS.yellow; break; case types_1.LogLevel.Fatal: color = COLORS.cyan; break; case types_1.LogLevel.Error: default: color = COLORS.red; } const toLog = []; toLog.push(`${COLORS.dim}${new Date().toTimeString().split(' ')[0]}${COLORS.reset} ` + color + COLORS.bold + `[${level.toUpperCase()}]:`.padEnd(9) + COLORS.reset + message); if (extra) toLog.push(...getExtra(extra)); console.log(toLog.join('\n')); }; exports.default = developmentLogger; //# sourceMappingURL=dev-logger.js.map