UNPKG

@loglayer/context-manager

Version:

Base context manager used to implement context managers for loglayer.

80 lines (78 loc) 1.82 kB
// src/DefaultContextManager.ts var DefaultContextManager = class _DefaultContextManager { context = {}; hasContext = false; /** * Sets the context data to be included with every log entry. Set to `undefined` to clear the context data. */ setContext(context) { if (!context) { this.context = {}; this.hasContext = false; return; } this.context = context; this.hasContext = true; } /** * Appends context data to the existing context data. */ appendContext(context) { this.context = { ...this.context, ...context }; this.hasContext = true; } /** * Returns the context data to be included with every log entry. */ getContext() { return this.context; } /** * Returns true if context data is present. */ hasContextData() { return this.hasContext; } /** * Copies the parent context data to the child context data. */ onChildLoggerCreated({ parentContextManager, childContextManager }) { if (parentContextManager.hasContextData()) { const parentContext = parentContextManager.getContext(); childContextManager.setContext({ ...parentContext }); } } /** * Creates a new instance of the context manager with the same context data. */ clone() { const clone = new _DefaultContextManager(); clone.setContext({ ...this.context }); clone.hasContext = this.hasContext; return clone; } }; // src/MockContextManager.ts var MockContextManager = class _MockContextManager { setContext(_context) { } appendContext(_context) { } getContext() { return {}; } hasContextData() { return false; } onChildLoggerCreated(_params) { } clone() { return new _MockContextManager(); } }; export { DefaultContextManager, MockContextManager };