Skip to content

aquariuslt/nest-rabbitmq-appender

Repository files navigation

NestJS RabbitMQ Logger Appender

NPM Github Workflow Status Codecov Semantic-Release

Provide a rabbitmq logger appender like logback options style.

Inspired by spring-amqp project

Warning: In Beta

Usage

Installation

yarn add nest-rabbitmq-appender

Register Module

You should pass an AMQP connection option as register RabbitmqAppenderModule options at least with url and queue.

export type RabbitmqAppenderOptions = {
  url: string; // rabbitmq connection url
  queue: string; // rabbitmq logging queue
} & Partial<Options.Connect>;
import { Module } from '@nestjs/common';
import { RabbitmqAppenderModule } from 'nest-rabbitmq-appender';
import { ConfigService } from '@/config/config.service';
import { RemoteLogger } from '@/remote.logger';

@Module({
  imports: [
    ConfigModule,
    RabbitmqAppenderModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => config.loggingAppenderOptions // config.loggingAppenderOptions suppose be type of `RabbitmqAppenderOptions`
    })
  ]
})
export class AppModule {}

Use RemoteLogger extending NestJS Bundled logger

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { RemoteLogger } from 'nest-rabbitmq-appender';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: false
  });
  app.useLogger(app.get(RemoteLogger));

  await app.listen(3000);
}
bootstrap();

Advanced Usage: Add Customize Data as HookFunction

RemoteLogger provided a public function: setHookFunction(hookFn) for advanced usage.

Mostly you want to add additional data when sending log data to remote message queue.

Like environment variables set from kubernetes (POD_ID,POD_UID...), or NODE_ENV in node.js application, or some dynamic value base on runtime status.

You should use an hookFunction to create additional properties when send to rabbitmq.

function hookFunction(message?:string, context?string){
  return {
    // calculate and return data according to your business logic
  }
}
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { RemoteLogger } from 'nest-rabbitmq-appender';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: false
  });
  
  const remoteLogger = app.get(RemoteLogger);
  const hookFn = () => {
    return {
      NODE_ENV: process.env.NODE_ENV,
      NODE_IP: process.env.NODE_IP,
      // or any other data in context, it will merge with origin message
    }    
  };

  remoteLogger.setHookFunction(hookFn);
  app.useLogger(remoteLogger);

  await app.listen(3000);
}
bootstrap();