📘
@node-test/bus
  • @node-ts/bus
  • Getting started
    • Installation
    • Handling messages
    • Shutting down cleanly
  • Reference
    • Bus
    • BusConfiguration
    • BusInstance
  • Getting help
  • Guide
    • Messages
      • Events
      • Commands
      • System messages
    • Message attributes
      • Correlation id
      • Attributes
      • Sticky attributes
    • Workflows
      • Creating a workflow
      • Starting
      • Handling
      • State
      • Completing
      • Example
    • Transports
      • RabbitMQ
      • Amazon SQS
      • Redis
      • Custom transports
    • Persistence
      • Postgres
      • MongoDB
      • Creating a persistence
    • Serializers
      • Class serializer
    • Loggers
      • Custom loggers
    • Middleware
    • Lifecycle hooks
    • Retry Strategies
    • Dependency injection
    • Long running processes
Powered by GitBook
On this page

Was this helpful?

  1. Guide
  2. Loggers

Custom loggers

PreviousLoggersNextMiddleware

Last updated 3 years ago

Was this helpful?

To use your own logger with @node-ts/bus, create an adapter class by implementing the Logger interface. In this example is being used

import { Logger } from '@node-ts/bus-core'
import winston from 'winston'

export class WinstonLogger implements Logger {

  private winstonLogger: winston.Logger

  constructor (
    name: string // This is the name of the class the logger will be injected into
  ) {
    this.winstonLogger = winston.createLogger({
      format: winston.format.json(),
      defaultMeta: { name }
    })
  }

  debug (message: string, meta?: object): void {
    this.winstonLogger.debug(message, meta)
  }

  trace (message: string, meta?: object): void {
    this.winstonLogger.verbose(message, meta)
  }

  info (message: string, meta?: object): void {
    this.winstonLogger.info(message, meta)
  }

  warn (message: string, meta?: object): void {
    this.winstonLogger.warn(message, meta)
  }

  error (message: string, meta?: object): void {
    this.winstonLogger.error(message, meta)
  }

  fatal (message: string, meta?: object): void {
    this.winstonLogger.crit(message, meta)
  }
}

Configure @node-ts/bus-core to use your log adapter

import { Bus } from '@node-ts/bus-core'
import { WinstonLogger } from './winston-logger'
​
async function run () {
  const bus = await Bus.configure()
    .withLogger((target: string) => new WinstonLogger(target))
    .initialize()
}
winston