Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-pino-stackdriver

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-pino-stackdriver

## Using the logger Configured to be stackdriver compliant.

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

NestJS Stackdriver

Using the logger

Configured to be stackdriver compliant.

Using as default logger

import { Logger } from 'nestjs-pino-stackdriver';
// In your main JS
async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new Logger(),
  });
  await app.listen(3000);
}
bootstrap();

Using inside the app

Import it in your module

Configure it using pino options https://github.com/pinojs/pino/blob/master/docs/api.md

import { LoggerModule } from 'nestjs-pino-stackdriver';
@Module({
  // You can leave config empty
  imports: [LoggerModule.forRoot({ name: 'example' })],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
}
Inject it from everywhere
import { Controller, Get, Logger } from '@nestjs/common';
@Controller()
export class AppController {
  constructor(
    private readonly logger: Logger,
  ) {
    this.logger.log('app controller booted');
  }

  @Get()
  getHello(): string {
    this.logger.log(headers);
    return 'hello';
  }
}

Correlation ID and optionnal tracer

Add in your request a correlation id if none provided

import { CorrelationTracerMiddleware } from 'nestjs-pino-stackdriver';
async function bootstrap() {
  app.use(
    CorrelationTracerMiddleware({
      // To generate logger on each request and inject correlation id
      app: app,
      // Optionnal to start tracing
      agent: require('@google-cloud/trace-agent').start(),
    }),
  );

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

Use log with trace and correlation id

import { Controller, Get, Headers, , Query } from '@nestjs/common';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
  ) {}

  @Get()
  getHello(@Headers() headers, @Query('logger') logger): string {
    logger.log('request received', 'app.getHello', headers);
    return this.appService.getHello();
  }
}

Output in production

 {"severity":"info","correlation_id":"4NTtNp4X","logging.googleapis.com/trace":"281fe620078d4562b311712cf42faefd","context":"app.getHello","message":"request received {\"host\":\"localhost:3000\",\"user-agent\":\"curl/7.64.1\",\"accept\":\"*/*\",\"x-correlation-id\":\"4NTtNp4X\"}","v":1}

Or locally

[2020-03-03 19:24:00.673 +0000] INFO : request received {"host":"localhost:3000","user-agent":"curl/7.64.1","accept":"*/*","x-correlation-id":"51eLtfXz"}
       correlation_id: "51eLtfXz"
       logging.googleapis.com/trace: "9b25e09ac8be4e0c93ba40304bf80fe8"
       context: "app.getHello"

Keywords

FAQs

Package last updated on 10 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc