lambda-monitor-logger
Version:
Logging designed to be used with lambda-monitor
31 lines (26 loc) • 778 B
JavaScript
import abbrev from './abbrev.js';
const logging = (type, msg) => {
// eslint-disable-next-line no-console
console.log(`${type.toUpperCase()}: ${typeof msg === 'string' ? msg : abbrev(msg)}`);
};
const levelMap = {
debug: ['debug', 'trace'],
info: ['info', 'log'],
warning: ['warning', 'warn'],
error: ['error', 'err'],
critical: ['critical', 'fatal']
};
const levels = Object.keys(levelMap);
export default Object.entries(levelMap).reduce(
(prev, [level, names], idx) => Object.assign(prev, names.reduce(
(p, name) => Object.assign(p, {
[name]: (...msgs) => {
if (levels.indexOf((process.env.LOG_LEVEL || 'debug').toLowerCase()) <= idx) {
msgs.forEach((msg) => logging(level, msg));
}
}
}),
{}
)),
{}
);