NestJS GCP Logging: Seamlessly Integrate Google Cloud Platform Logging

Nest.js GCP Logger

Module Parameters

PropertyDefaultDescription
gcpErrorReportingfalseIf 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

Update module creation

  • from
LoggingModule.register()
  • to
LoggingModule.forRoot()

Update name of param

  • from
GCP_ERROR_REPORTING
  • to
gcpErrorReporting

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))

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!';
  }
}

Download Details:

Author: mr-pascal
Source Code: https://github.com/mr-pascal/nestjs-gcp-logging 
License: ISC

#nestjs #gcp #log 

NestJS GCP Logging: Seamlessly Integrate Google Cloud Platform Logging
2.05 GEEK