@loglayer/context-manager
Version:
Base context manager used to implement context managers for loglayer.
80 lines (75 loc) • 2.09 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); var _class;// src/DefaultContextManager.ts
var DefaultContextManager = (_class = class _DefaultContextManager {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this); }
__init() {this.context = {}}
__init2() {this.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;
}
}, _class);
// src/MockContextManager.ts
var MockContextManager = class _MockContextManager {
setContext(_context) {
}
appendContext(_context) {
}
getContext() {
return {};
}
hasContextData() {
return false;
}
onChildLoggerCreated(_params) {
}
clone() {
return new _MockContextManager();
}
};
exports.DefaultContextManager = DefaultContextManager; exports.MockContextManager = MockContextManager;