Plugins
LogLayer's plugin system allows you to extend and modify logging behavior at various points in the log lifecycle. Plugins can modify data and messages before they're sent to the logging library, control whether logs should be sent, and intercept metadata calls.
Available Plugins
Name | Package | Changelog | Description |
---|---|---|---|
Filter | Changelog | Filter logs using string patterns, regular expressions, or JSON Queries | |
OpenTelemetry | Changelog | Add OpenTelemetry trace context to logs | |
Sprintf | Changelog | Printf-style string formatting support | |
Redaction | Changelog | Redact sensitive information from logs |
Plugin Management
Adding Plugins
You can add plugins when creating the LogLayer instance:
const log = new LogLayer({
transport: new ConsoleTransport({
logger: console
}),
plugins: [
timestampPlugin(),
{
// id is optional
id: 'sensitive-data-filter',
onBeforeDataOut(params) {
// a simple plugin that does something
return params.data
}
}
]
})
Or add them later:
log.addPlugins([timestampPlugin()])
log.addPlugins([{
onBeforeDataOut(params) {
// a simple plugin that does something
return params.data
}
}])
Enabling/Disabling Plugins
Plugins can be enabled or disabled at runtime using their ID (if defined):
// Disable a plugin
log.disablePlugin('sensitive-data-filter')
// Enable a plugin
log.enablePlugin('sensitive-data-filter')
Removing Plugins
Remove a plugin using its ID (if defined):
log.removePlugin('sensitive-data-filter')
Replacing All Plugins
Replace all existing plugins with new ones:
log.withFreshPlugins([
timestampPlugin(),
{
onBeforeDataOut(params) {
// do something
return params.data
}
}
])
When used with child loggers, this only affects the current logger instance and does not modify the parent's plugins.
Potential Performance Impact
Replacing plugins at runtime may have a performance impact if you are frequently creating new plugins. It is recommended to re-use the same plugin instance(s) where possible.