Context Managers
New in LogLayer v6.
Context managers in LogLayer are responsible for managing contextual data that gets included with log entries. They provide a way to store and retrieve context data that will be automatically included with every log message.
Do you need a context manager?
Context managers are an advanced feature of LogLayer.
Unless you need to manage context data in a specific way, you can use the default context manager, which is already automatically used when creating a new LogLayer instance.
Available Context Managers
Name | Package | Description |
---|---|---|
Default | Base context manager that provides independent context for each logger instance | |
Linked | Context manager that keeps context synchronized between parent and all children |
Context Manager Management
Using a custom context manager
You can set a custom context manager using the withContextManager()
method.
Example usage:
import { MyCustomContextManager } from './MyCustomContextManager';
const logger = new LogLayer()
.withContextManager(new MyCustomContextManager());
TIP
Use the withContextManager()
method right after creating the LogLayer instance. Using it after the context has already been set will drop the existing context data.
Obtaining the current context manager
You can get the current context manager instance using the getContextManager()
method:
const contextManager = logger.getContextManager();
You can also type the return value when getting a specific context manager implementation:
const linkedContextManager = logger.getContextManager<LinkedContextManager>();