Skip to content

mr-pascal/nestjs-gcp-logging

Repository files navigation

Nest.js GCP Logger

Module Parameters

Property Default Description
gcpErrorReporting false If set to true all error messages are recognized by GCP Error Reporting by wrapping the provided message in a stack trace

How to use

// app.module.ts
import { Module } from '@nestjs/common';
import { LoggingModule } from '@pzwik/nestjs-gcp-logger'; // <-- Import the module
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    LoggingModule.forRoot({ // <-- Initialize the module
      gcpErrorReporting: false // default is 'false'
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }
// main.ts
import { NestFactory } from '@nestjs/core';
import { LoggingService } from '@pzwik/nestjs-gcp-logger'; // <-- Import here
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useLogger(app.get(LoggingService));

  await app.listen(3000);
}
bootstrap();
import { Injectable, Logger } from '@nestjs/common'; // <-- Import Logger from Nest.js

@Injectable()
export class AppService {
  private readonly logger = new Logger('AppService');

  getHello(): string {
    this.logger.error('Some Error happened!'); // <-- Calls the logger
    // {"severity":"ERROR","message":"Some Error happened!"}
    return 'Hello World!';
  }
}

How to mock in tests

  // logger.mock.ts
  export class LoggerMock {
  log() {
    return;
  }
  error() {
    return;
  }
  warn() {
    return;
  }
  debug() {
    return;
  }
  verbose() {
    return;
  }
}
  // test.spec.ts
  const moduleFixture = await Test.createTestingModule({...})
    .overrideProvider(LoggingService)
    .useClass(LoggerMock)
    .compile();

  const app = moduleFixture.createNestApplication();

  // Do not forget to set the logger, otherwise nestjs default logger
  app.useLogger(app.get(LoggingService));
  
  await app.init();

Migrate 1.X -> 2.X

  1. Update module creation
  • from
LoggingModule.register()
  • to
LoggingModule.forRoot()
  1. Update name of param
  • from
GCP_ERROR_REPORTING
  • to
gcpErrorReporting
  1. Update how logger passed to app
  • from
const app = await NestFactory.create(AppModule, { logger: new LoggingService() });
  • to
const app = await NestFactory.create(AppModule, { bufferLogs: true });
app.useLogger(app.get(LoggingService))
  1. Update logger usage in services
  • from
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    Logger.error('Some Error happened!');
    return 'Hello World!';
  }
}
  • to
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class AppService {
  private readonly logger = new Logger('AppService');

  getHello(): string {
    this.logger.error('Some Error happened!');
    return 'Hello World!';
  }
}