simple-leveled-log-methods
Version:
a simple and opinionated logging library. plays well with aws lambda + cloudwatch.
37 lines • 1.61 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateLogMethod = void 0;
const constants_1 = require("./constants");
const formatLogContentsForEnvironment_1 = require("./formatLogContentsForEnvironment");
/*
define priority order of log levels and make it easy to ask questions about
*/
const logLevelPriorityOrder = [
constants_1.LogLevel.ERROR,
constants_1.LogLevel.WARN,
constants_1.LogLevel.INFO,
constants_1.LogLevel.DEBUG,
];
const aIsEqualOrMoreImportantThanB = ({ a, b }) => logLevelPriorityOrder.indexOf(a) - logLevelPriorityOrder.indexOf(b) <= 0;
const generateLogMethod = ({ level, minimalLogLevel, }) => {
return (message, metadata) => {
if (aIsEqualOrMoreImportantThanB({ a: level, b: minimalLogLevel })) {
// determine the console level (i.e., use warn if we can to make the logs stand out more)
const consoleMethod = aIsEqualOrMoreImportantThanB({
a: level,
b: constants_1.LogLevel.WARN,
})
? console.warn
: console.log; // tslint:disable-line no-console
// output the message to console, which will get picked up by cloudwatch when deployed lambda is invoked
consoleMethod((0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)({
level,
timestamp: new Date().toISOString(),
message,
metadata,
}));
}
};
};
exports.generateLogMethod = generateLogMethod;
//# sourceMappingURL=generateLogMethod.js.map
;