@toreda/log
Version:
Lightweight TypeScript logger with flexible custom transports.
44 lines (43 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transport = void 0;
const level_1 = require("./log/level");
/**
* Executes user-provided callback once for each message received.
* Only receives messages matching user-configured log levels and
* additional filters.
*
* @category Transports
*/
class Transport {
constructor({ id, level, action }) {
if (!id && typeof id !== 'string') {
throw new Error('Transport init failure - id arg is missing.');
}
if (typeof id !== 'string') {
throw new Error(`Transport init failure - id arg must be a non-empty string.`);
}
if (!action) {
throw new Error(`[logtr:${id}] Init failure - action arg is missing.`);
}
if (typeof action !== 'function') {
throw new Error(`[logtr:${id}] Init failure - action arg must be a function.`);
}
this.id = id;
this.action = action;
this.level = new level_1.LogLevel(level);
}
execute(msg) {
const action = new Promise((resolve) => {
resolve(this.action(msg));
});
return action
.then((res) => {
return res;
})
.catch((err) => {
return err;
});
}
}
exports.Transport = Transport;