@nest-gcr/logger
TypeScript icon, indicating that this package has built-in type declarations

1.0.17 • Public • Published

NestJS Logger for Google Cloud Run

Installation

  1. Install the dependency
yarn add @nest-gcr/logger
  1. Register the module
import { Module } from '@nestjs/common';
import { LoggerModule } from '@nest-gcr/logger';

@Module({
  imports: [LoggerModule]
})
export class AppModule {}

Usage

  1. Use it anywhere in your application. You have access to two providers:
import { Inject, Injectable } from '@nestjs/common';
import { LOGGER, Logger } from '@nest-gcr/logger'

@Injectable()
export class MyProvider {
  constructor(
    @Inject(LOGGER.PROVIDERS.LOGGER) logger: Logger,
    @Inject(LOGGER.PROVIDERS.REQUEST_LOGGER) requestLogger: Logger
  ) {}
  
  foo () {
    this.requestLogger.debug('Hello World!')
  }
}
  1. Use the global logger for default nestJS logs:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { rootLogger } from '@nest-gcr/logger';

async function bootstrap() {
  const app = await NestFactory.create(TestModule, {
    logger: {
      ...rootLogger,
      log: (message, parameters) => {
        return rootLogger.info(message, parameters);
      },
      error: (message, parameters) => {
        return rootLogger.error(message, parameters)
      }
    },
  });
  await app.listen(process.env.PORT || 3000);
}

bootstrap();

Configuration on Google Cloud RUN

In order for logs to be correctly logged to Google Cloud Logging from Cloud Run instances, you need to set the following environment variable into your container:

LOGGER_DRIVER=gcp

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Author

John Biundo (Y Prospect on Discord)

License

Licensed under the MIT License - see the LICENSE file for details.

Readme

Keywords

Package Sidebar

Install

npm i @nest-gcr/logger

Weekly Downloads

1

Version

1.0.17

License

MIT

Unpacked Size

45.6 kB

Total Files

35

Last publish

Collaborators

  • hammerbot