Logging Pipeline
Introduction
Section titled “Introduction”The Logging Pipeline logs the execution of commands and queries handled through the Xeno.JS CQRS pipeline.
It records:
- when a request starts;
- when a request completes successfully;
- when a request returns a failed
Result; - when request execution throws an exception.
The pipeline uses the application logger, so you can configure where logs are written without changing your commands or handlers.
Before you start
Section titled “Before you start”You need:
- an
XenoAppBuilderinstance; - a command or query handled through the Xeno.JS mediator;
- the CQRS pipeline enabled with
addPipeline().
You do not need to register the LoggingPipeline manually.
Enable logging
Section titled “Enable logging”Enable the CQRS pipeline through XenoAppBuilder:
import { XenoAppBuilder } from '@xeno-js/core'
const app = new XenoAppBuilder() .addPipeline()
const application = await app.build()When addPipeline() is configured, the CQRS module adds the Logging Pipeline as a common behavior. It also adds a logger if none has been registered.
Both commands and queries use the logging pipeline.
Configure the logger
Section titled “Configure the logger”Use addLogger() to configure the logger used by the application.
For example, to configure the minimum log level:
import { XenoAppBuilder } from '@xeno-js/core'import { LOG_LEVEL } from '@xeno-js/shared'
const app = new XenoAppBuilder() .addLogger((config) => { config.level = LOG_LEVEL.INFO }) .addPipeline()
const application = await app.build()The addLogger() setup callback configures core logger settings. Provider
options are configured through the provider’s logger plugin:
| Option | Description |
|---|---|
level |
Minimum log level forwarded to the configured logging clients. |
console |
Enables or disables console logging. |
customLoggers |
Registers custom logging clients. |
addLogger() also accepts a logger plugin or an array of plugins. If the CQRS
module needs a logger and none has been registered, it adds one during
application build. See the logger overview for logger
defaults and provider setup.
Choose the log level
Section titled “Choose the log level”Xeno.JS filters messages below the configured minimum level.
For example:
import { XenoAppBuilder } from '@xeno-js/core'import { LOG_LEVEL } from '@xeno-js/shared'
const app = new XenoAppBuilder() .addLogger((config) => { config.level = LOG_LEVEL.WARN }) .addPipeline()
const application = await app.build()With LOG_LEVEL.WARN, informational messages from the Logging Pipeline are filtered out, while warnings and errors remain available to logging clients.
The Logging Pipeline itself uses:
infofor request start;infofor successful completion;errorfor failedResultvalues;errorfor thrown exceptions.
Configure console logging
Section titled “Configure console logging”The default Console provider is enabled by default. The CQRS module adds a logger when needed, so logging works without manually registering one.
You can configure it explicitly:
const app = new XenoAppBuilder() .addLogger((config) => { config.console = true })
app.addPipeline()
const application = await app.build()To disable the console logger:
const app = new XenoAppBuilder() .addLogger((config) => { config.console = false })
app.addPipeline()
const application = await app.build()Disabling the Console provider does not disable the Logging Pipeline itself. Logs can still be sent to other configured logger clients.
Configure Pino
Section titled “Configure Pino”Register the Pino logger plugin from @xeno-js/pino. The plugin callback
receives Pino-specific options; these are separate from the core logger setup
callback:
import { XenoAppBuilder } from '@xeno-js/core'import { withPinoLogger } from '@xeno-js/pino'
const app = new XenoAppBuilder() .addLogger(withPinoLogger((options) => { options.destination = 'stdout' }))
app.addPipeline()
const application = await app.build()Write Pino logs to a file
Section titled “Write Pino logs to a file”Set destination to file and provide a file path through the Pino plugin:
import { XenoAppBuilder } from '@xeno-js/core'import { withPinoLogger } from '@xeno-js/pino'
const app = new XenoAppBuilder() .addLogger(withPinoLogger((options) => { options.destination = 'file' options.filePath = 'logs/app.log' }))Supported destinations are:
stdout;file.
If filePath is omitted when using the file destination, Xeno.JS uses
logs/app.log.
For Pino behavior, including formatting, defaults, and redaction, see Pino Logger. Those options belong to the Pino integration, not to the core logger configuration.
Configure Sentry
Section titled “Configure Sentry”Register the Sentry Node logger plugin from @xeno-js/sentry-node. Sentry
provider options belong to that plugin; the core logger setup callback does
not configure them.
Configure the Sentry DSN:
import { XenoAppBuilder } from '@xeno-js/core'import { withSentryNodeLogger } from '@xeno-js/sentry-node'
const app = new XenoAppBuilder() .addLogger(withSentryNodeLogger((options) => { options.dsn = process.env.SENTRY_DSN ?? '' options.environment = process.env.NODE_ENV }))
app.addPipeline()
const application = await app.build()The Sentry Node integration requires a DSN when enabled. Its environment and other provider-specific behavior are described in Sentry Node Logger.
Use multiple logging clients
Section titled “Use multiple logging clients”You can configure more than one logging destination.
For example, console and Pino can be enabled together:
import { XenoAppBuilder, withConsoleLogger } from '@xeno-js/core'import { withPinoLogger } from '@xeno-js/pino'import { LOG_LEVEL } from '@xeno-js/shared'
const app = new XenoAppBuilder() .addLogger([ withConsoleLogger((options) => { options.level = LOG_LEVEL.INFO }), withPinoLogger((options) => { options.destination = 'stdout' }), ]) .addPipeline()
const application = await app.build()Pass provider plugins together in one addLogger() call. The application
logger forwards each accepted message to the configured logging clients.
Add a custom logger
Section titled “Add a custom logger”Use customLoggers when you need to integrate a logging destination that is not provided by Xeno.JS.
A custom logger must implement the ILoggerClient contract from @xeno-js/shared.
For example:
import type { ILoggerClient } from '@xeno-js/shared'
const customLogger = (): ILoggerClient => ({ track(level, message, context, error) { console.log({ level, message, context, error, }) },})Register it with addLogger():
const app = new XenoAppBuilder() .addLogger((config) => { config.customLoggers = [ () => customLogger(), ] })
app.addPipeline()
const application = await app.build()Custom logger factories use the scope contract described in the logger overview when they need service resolution.
What does the Logging Pipeline log?
Section titled “What does the Logging Pipeline log?”For a request such as:
{ type: 'COMMAND', intent: 'CREATE_USER'}the pipeline logs the start of the request:
Handling COMMAND CREATE_USERWhen the request succeeds:
Successfully handled COMMAND CREATE_USERWhen the handler returns a failed Result:
Failed to handle COMMAND CREATE_USER: User already existsWhen request execution throws:
Exception while handling COMMAND CREATE_USERThe failed Result is returned unchanged.
The Logging Pipeline logs a thrown value and rethrows it. When the CQRS
Exception Pipeline is also active, it can convert that thrown value into a
failed Result; see Exception Pipeline.
Logging failed Results
Section titled “Logging failed Results”A command or query can complete without throwing but still return a failed Result.
For example:
const result = await mediator.send(command)
if (!result.isOk()) { // The Logging Pipeline has already logged the failure.}The Logging Pipeline does not replace or transform the Result.
It logs the error and returns the same result to the caller.
Logging thrown exceptions
Section titled “Logging thrown exceptions”If execution throws an exception, the Logging Pipeline logs it and rethrows it
to the surrounding CQRS behavior. With the Exception Pipeline enabled, the
mediator returns a failed Result:
const result = await mediator.send(command)
if (!result.isOk()) { // The failure is available as a Result.}The Logging Pipeline does not convert exceptions itself; the CQRS Exception Pipeline handles that conversion.
Complete example
Section titled “Complete example”A typical application can configure logging and the CQRS pipeline together:
import { XenoAppBuilder, withConsoleLogger } from '@xeno-js/core'import { withPinoLogger } from '@xeno-js/pino'import { LOG_LEVEL } from '@xeno-js/shared'
const app = new XenoAppBuilder() .addLogger([ withConsoleLogger((options) => { options.level = LOG_LEVEL.INFO }), withPinoLogger((options) => { options.destination = 'stdout' }), ]) .addPipeline()
const application = await app.build()After the application is built, commands and queries executed through the configured mediator are logged automatically.
Logging is not running
Section titled “Logging is not running”If commands or queries are not producing logs, check:
addPipeline()is called;- the command or query is executed through the Xeno.JS mediator;
- console logging has not been disabled with
config.console = false; - the configured minimum
config.leveldoes not filter the messages you expect; - at least one logging client is configured.
For example, if you configure:
config.level = LOG_LEVEL.WARNthe normal info messages generated by the Logging Pipeline will not be forwarded.
Pino logs are not written to the expected destination
Section titled “Pino logs are not written to the expected destination”Check that the Pino plugin is passed to addLogger() and configure its
destination in the plugin callback:
app.addLogger(withPinoLogger((options) => { options.destination = 'file' options.filePath = 'logs/app.log'}))For a file destination:
destinationmust befile;filePathshould point to the desired output file.
For standard output:
app.addLogger(withPinoLogger((options) => { options.destination = 'stdout'}))Sentry events are not appearing
Section titled “Sentry events are not appearing”Check that the Sentry Node plugin is registered and that it receives a DSN:
import { withSentryNodeLogger } from '@xeno-js/sentry-node'
app.addLogger(withSentryNodeLogger((options, config) => { options.dsn = config.get('SENTRY_DSN') ?? ''}))For the Sentry plugin’s configuration and environment-variable behavior, see Sentry Node Logger.
See Loggers Documentation
Section titled “See Loggers Documentation”Related docs
Section titled “Related docs”- Application overview
- Exception Pipeline
- Performance Pipeline
- Creating Commands
- Creating Queries
- Creating Handlers
- Loggers Documentation
Support Us
Section titled “Support Us”Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section
