0.3.2 • Published 3 days ago

@hyperdx/node-logger v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

HyperDX Node.js Logger

Node.js Logging Library for HyperDX

Installation

Install the Node.js logger into your project.

npm install @hyperdx/node-logger

or

yarn add @hyperdx/node-logger

Setup

Before starting with the code, make sure to obtain your API key by logging into/signing up for HyperDX and visiting your team page: https://hyperdx.io/team. This key is necessary for sending logs to your account.

Winston Transport

Create a new HyperDX Winston Transport and append it to your list of transports. Example:

import winston from 'winston';
import { HyperDXWinston } from '@hyperdx/node-logger';

const hyperdxTransport = new HyperDXWinston({
  apiKey: ***HYPERDX_API_KEY***,
  maxLevel: 'info',
  service: 'my-app',
});


const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console(),
    hyperdxTransport, // append this to the existing transports
  ],
});

export default logger;

Pino Transport

Create a new HyperDX Pino Transport and append it to your list of transports. Example:

import pino from 'pino';

const logger = pino(
  pino.transport({
    targets: [
      {
        target: '@hyperdx/node-logger/build/src/pino',
        options: {
          apiKey: ***HYPERDX_API_KEY***,
          service: 'my-app',
        },
        level: 'info',
      },
      // other transports
    ],
  }),
);

export default logger;

NestJS Custom Logger

(powered by nest-winston)

Import HyperDXNestLoggerModule into the root AppModule and use the forRoot() method to configure it.

import { Module } from '@nestjs/common';
import { HyperDXNestLoggerModule } from '@hyperdx/node-logger';

@Module({
  imports: [
    HyperDXNestLoggerModule.forRoot({
      apiKey: ***HYPERDX_API_KEY***,
      maxLevel: 'info',
      service: 'my-app',
    }),
  ],
})
export class AppModule {}

Afterward, the winston instance will be available to inject across entire project using the HDX_LOGGER_MODULE_PROVIDER injection token:

import { Controller, Inject } from '@nestjs/common';
import { HyperDXNestLoggerModule, HyperDXNestLogger } from '@hyperdx/node-logger';

@Controller('cats')
export class CatsController {
  constructor(
    @Inject(HyperDXNestLoggerModule.HDX_LOGGER_MODULE_PROVIDER)
    private readonly logger: HyperDXNestLogger,
  ) { }

  meow() {
    this.logger.info({ message: '🐱' });
  }
}

Replacing the Nest logger (also for bootstrapping)

Important: by doing this, you give up the dependency injection, meaning that forRoot and forRootAsync are not needed and shouldn't be used. Remove them from your main module.

Using the dependency injection has one minor drawback. Nest has to bootstrap the application first (instantiating modules and providers, injecting dependencies, etc.) and during this process the instance of HyperDXNestLogger is not yet available, which means that Nest falls back to the internal logger.

One solution is to create the logger outside of the application lifecycle, using the createLogger function, and pass it to NestFactory.create. Nest will then wrap our custom logger (the same instance returned by the createLogger method) into the Logger class, forwarding all calls to it:

Create the logger in the main.ts file

import { HyperDXNestLoggerModule } from '@hyperdx/node-logger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: HyperDXNestLoggerModule.createLogger({
      apiKey: ***HYPERDX_API_KEY***,
      maxLevel: 'info',
      service: 'my-app',
    })
  });
  await app.listen(3000);
}
bootstrap();

Change your main module to provide the Logger service:

import { Logger, Module } from '@nestjs/common';

@Module({
  providers: [Logger],
})
export class AppModule {}

Then inject the logger simply by type hinting it with Logger from @nestjs/common:

import { Controller, Logger } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  constructor(private readonly logger: Logger) {}

  meow() {
    this.logger.log({ message: '🐱' });
  }
}
0.4.0-next.1

3 days ago

0.4.0-next.0

6 days ago

0.3.2-next.0

13 days ago

0.3.2

12 days ago

0.3.1

4 months ago

0.3.0

4 months ago

0.3.0-next.0

5 months ago

0.2.9-next.1

5 months ago

0.2.9-next.0

5 months ago

0.2.9

5 months ago

0.2.4-next.0

10 months ago

0.2.7

8 months ago

0.2.6

9 months ago

0.2.7-next.0

9 months ago

0.2.8

8 months ago

0.2.5

9 months ago

0.2.4

10 months ago

0.0.10

12 months ago

0.1.0

12 months ago

0.2.1

11 months ago

0.2.0

12 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.2.3

11 months ago

0.2.2

11 months ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago