@obsidize/logger
Version:
A tiny javascript logging library
58 lines (57 loc) • 2.08 kB
JavaScript
import { EventEmitter } from './event-emitter';
import { LogEvent } from './log-event';
import { LogEventGuard } from './log-event-guard';
import { Logger } from './logger';
/**
* Route controller for events.
*
* When a logger referencing this transport (via `getLogger(tag)`) produces an event,
* the event will be verified against the transport's filter; if the event is
* deemed valid, it will be passed on to all assigned consumers, as well as
* any listeners registered to the transport's event emitter.
*/
export class LogEventTransport extends LogEventGuard {
constructor(options = {}) {
super();
this.events = new EventEmitter();
this.forwardRef = this.interceptEvent.bind(this);
this.configure(options);
}
getLogger(tag) {
return new Logger(tag, this);
}
interceptEvent(ev) {
if (this.test(ev))
this.events.emit(ev);
}
createEvent(level, tag, message, params, timestamp) {
return new LogEvent(level, tag, message, params, timestamp);
}
configure(config) {
var _a;
this.events.removeAllListeners();
const inputs = config.inputs;
const outputs = config.outputs;
if (typeof config.filter === 'function') {
this.setCustomFilter(config.filter);
}
if (Array.isArray(outputs)) {
for (const output of outputs) {
if (typeof output === 'function') {
this.events.addListener(output);
}
}
}
if (Array.isArray(inputs)) {
for (const input of inputs) {
if (typeof input === 'function') {
input(this.forwardRef);
}
else if (typeof input === 'object' &&
typeof ((_a = input === null || input === void 0 ? void 0 : input.events) === null || _a === void 0 ? void 0 : _a.addListener) === 'function') {
input.events.addListener(this.forwardRef);
}
}
}
}
}