UNPKG

logsdx

Version:

log streaming with dx on the 🧠

127 lines (126 loc) • 2.98 kB
// src/logenhancer.ts var LogEnhancer = class { plugins = []; parsers = []; clients = []; debug; constructor(options = {}) { this.debug = options.debug ?? false; if (options.plugins) { this.loadPlugins(options.plugins); } if (options.parsers) { this.loadParsers(options.parsers); } if (options.clients) { this.loadClients(options.clients); } } async loadPlugins(plugins) { for (const plugin of plugins) { if (typeof plugin === "string") { try { const mod = await import(plugin); this.use(mod.default); } catch (err) { if (this.debug) { console.error(`Failed to load plugin: ${plugin}`, err); } } } else { this.use(plugin); } } } async loadParsers(parsers) { for (const parser of parsers) { if (typeof parser === "string") { try { const mod = await import(parser); this.addParser(mod.default); } catch (err) { if (this.debug) { console.error(`Failed to load parser: ${parser}`, err); } } } else { this.addParser(parser); } } } async loadClients(clients) { for (const client of clients) { if (typeof client === "string") { try { const mod = await import(client); this.addClient(mod.default); } catch (err) { if (this.debug) { console.error(`Failed to load client: ${client}`, err); } } } else { this.addClient(client); } } } use(plugin) { if (this.debug) { console.log(`Adding plugin: ${plugin.name}`); } this.plugins.push(plugin); } addParser(parser) { if (this.debug) { console.log(`Adding parser: ${parser.name}`); } this.parsers.push(parser); } addClient(client) { if (this.debug) { console.log(`Adding client: ${client.name}`); } this.clients.push(client); } process(line) { const context = this.parsers.reduce((acc, parser) => { try { return { ...acc, ...parser.parse(line) }; } catch (err) { if (this.debug) { console.error(`Parser error in ${parser.name}:`, err); } return acc; } }, {}); let result = line; for (const plugin of this.plugins) { try { result = plugin.enhance(result); } catch (err) { if (this.debug) { console.error(`Plugin error in ${plugin.name}:`, err); } } } for (const client of this.clients) { try { client.write(result); } catch (err) { if (this.debug) { console.error(`Client error in ${client.name}:`, err); } } } return result; } reset() { this.plugins = []; this.parsers = []; this.clients = []; } }; export { LogEnhancer }; //# sourceMappingURL=chunk-GEV5T5GP.js.map