@tazgr/nestjs-gcp-logger
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

nestjs-gcp-logger

XKCD 2863

It's a simple logger that is compatible with Google Cloud Logging https://cloud.google.com/logging?hl=en and https://cloud.google.com/products/operations?hl=en (aka tracing).

It should be plug and play when using a native service like Cloud Run.

Installation

npm install --save @tazgr/nestjs-gcp-logger

Usage

Basic

You must assign GCPLoggerService in your main.ts bootstrap() method. If it's not setup you will get the default logs/logger.

async function bootstrap() {
  const app = await NestFactory.create(AppModule.register(), { bufferLogs: true });
  // Important!
  app.useLogger(app.get(GCPLoggerService));
  await app.listen(3000);
}

In your app.module.ts file add: (this is a good sample for Cloud Run :) )

import { Module } from '@nestjs/common';
import { GCPLoggingModule } from '@tazgr/nestjs-gcp-logger';
import { env } from 'node:env';

@Module({
  imports: [GCPLoggingModule.register({
    isGlobal: true, // NestJS Global Module... I recommend leaving it true.
    projectId: 'your-project-id-from-gcp',
    logName: env.K_SERVICE,
    // https://cloud.google.com/run/docs/container-contract#env-vars
    // Use this is you are running some non Cloud Native apps like Compute Engine
    // or from external host provider (other than Google)
    // false means it will write the logs in stdout.
    isAsyncLogger: false,
    resourceType: 'cloud_run_revision',
    resourceLabels: {
      service_name: env.K_SERVICE,
      revision_name: env.K_REVISION,
      location: 'eu-west1',
      configuration_name: env.K_CONFIGURATION,
    }
  })]
})
export class AppModule {}

There is also a typical registerAsync version to inject config services etc...

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { GCPLoggingModule } from '@tazgr/nestjs-gcp-logger';

@Module({
  imports: [
    ConfigModule.forRoot({ ... }),
    GCPLoggingModule.registerAsync({
      inject: [ConfigService], // or something else, you do you
      useFactory: (config: ConfigService) => {
        const projectId = config.get<string>('PROJECT_ID_OR_WHATEVER');
        const logName = config.get<string>('LOGNAME_OR_WHATEVER');
        return {
          global: true,
          projectId,
          logName,
          isAsyncLogger: false,
        }
      }
    })
  ]
})
export class AppModule {}

Enjoy!

Links

Inspiration for find how to fill value in the configuration

Package Sidebar

Install

npm i @tazgr/nestjs-gcp-logger

Weekly Downloads

14

Version

1.0.7

License

MIT

Unpacked Size

146 kB

Total Files

21

Last publish

Collaborators

  • tazgr