syntropylog
Version:
An instance manager with observability for Node.js applications
111 lines • 4.47 kB
JavaScript
/**
* @file src/SyntropyLog.ts
* @description The main public-facing singleton class for the SyntropyLog framework.
* This class acts as a Facade, providing a simple and clean API surface
* while delegating all complex lifecycle and orchestration work to the internal
* LifecycleManager.
*/
import { EventEmitter } from 'events';
import { LifecycleManager } from './core/LifecycleManager';
/**
* @class SyntropyLog
* @description The main public entry point for the framework. It follows the
* Singleton pattern and acts as an EventEmitter to report on its lifecycle,
* proxying events from its internal LifecycleManager.
*/
export class SyntropyLog extends EventEmitter {
static instance;
lifecycleManager;
constructor() {
super();
this.lifecycleManager = new LifecycleManager(this);
// Proxy events from the lifecycle manager to the public facade
this.lifecycleManager.on('ready', () => this.emit('ready'));
this.lifecycleManager.on('error', (err) => this.emit('error', err));
this.lifecycleManager.on('shutting_down', () => this.emit('shutting_down'));
this.lifecycleManager.on('shutdown', () => this.emit('shutdown'));
}
static getInstance() {
if (!SyntropyLog.instance) {
SyntropyLog.instance = new SyntropyLog();
}
return SyntropyLog.instance;
}
getState() {
return this.lifecycleManager.getState();
}
async init(config) {
return this.lifecycleManager.init(config);
}
async shutdown() {
return this.lifecycleManager.shutdown();
}
getLogger(name = 'default', bindings) {
if (!this.lifecycleManager.loggerFactory) {
throw new Error('Logger Factory not available.');
}
return this.lifecycleManager.loggerFactory.getLogger(name, bindings);
}
async getRedis(name) {
this.lifecycleManager.ensureReady();
if (!this.lifecycleManager.redisManager) {
throw new Error('Redis manager not available. Make sure Redis is configured and redis package is installed.');
}
return this.lifecycleManager.redisManager.getInstance(name);
}
getHttp(name) {
this.lifecycleManager.ensureReady();
return this.lifecycleManager.httpManager.getInstance(name);
}
getBroker(name) {
this.lifecycleManager.ensureReady();
return this.lifecycleManager.brokerManager.getInstance(name);
}
getContextManager() {
this.lifecycleManager.ensureReady();
return this.lifecycleManager.contextManager;
}
getConfig() {
this.lifecycleManager.ensureReady();
return this.lifecycleManager.config;
}
getFilteredContext(level) {
this.lifecycleManager.ensureReady();
return this.lifecycleManager.contextManager.getFilteredContext(level);
}
/**
* Reconfigures the logging matrix dynamically.
* This method allows changing which context fields are included in logs
* without affecting security configurations like masking or log levels.
* @param matrix The new logging matrix configuration
*/
reconfigureLoggingMatrix(matrix) {
this.lifecycleManager.ensureReady();
this.lifecycleManager.contextManager.reconfigureLoggingMatrix(matrix);
}
getMasker() {
if (!this.lifecycleManager.maskingEngine) {
throw new Error('MaskingEngine not available.');
}
return this.lifecycleManager.maskingEngine;
}
getSerializer() {
if (!this.lifecycleManager.serializerRegistry) {
throw new Error('SerializerRegistry not available.');
}
return this.lifecycleManager.serializerRegistry;
}
_resetForTesting() {
// This needs to re-create the lifecycle manager to properly reset state
this.lifecycleManager.removeAllListeners();
this.lifecycleManager = new LifecycleManager(this);
this.removeAllListeners();
this.lifecycleManager.on('ready', () => this.emit('ready'));
this.lifecycleManager.on('error', (err) => this.emit('error', err));
this.lifecycleManager.on('shutting_down', () => this.emit('shutting_down'));
this.lifecycleManager.on('shutdown', () => this.emit('shutdown'));
}
}
/** The singleton instance of the SyntropyLog framework. */
export const syntropyLog = SyntropyLog.getInstance();
//# sourceMappingURL=SyntropyLog.js.map