4.0.0-alpha.42 • Published 3 months ago

@concepta/nestjs-logger v4.0.0-alpha.42

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
3 months ago

Rockets NestJS Logger

This module is a drop-in replacement for the core NestJS logger that provides additonal support for pushing log data to one or multiple external log consumption providers.

Project

NPM Latest NPM Downloads GH Last Commit GH Contrib NestJS Dep

Overview

This module wraps/extends the core NestJS Logger and adds a powerful external transports plugin interface.

See the NestJS Logger documentation for more details on how logging is implemented in NestJS.

Installation

yarn add @concepta/nestjs-logger

Registering

To start using the Logger Module, import the LoggerModule into your app.

Defaults (.env)

To register using the default configuration:

@Module({
  imports: [
    LoggerModule.register()
  ]
});
export class App {}

To use the default configuration, you need todefine the environments variables. One of the ways you can do that is using .env file

// .env file

LOG_LEVEL="log,error"
TRANSPORT_LOG_LEVEL="error,warn"
SENTRY_DSN="{your_sentry_dsn}"

Synchronous Registration

To register by direct configuration:

// ...
import { LoggerModule } from '@concepta/nestjs-logger';

@Module({
  imports: [
    LoggerModule.register({
      // ...
    })
  ]
});
export class App {}

Aynchronous Registration

To register by direct configuration:

// ...
import { LoggerModule } from '@concepta/nestjs-logger';

@Module({
  imports: [
    LoggerModule.registerAsync({
      imports: [ConfigModule.forFeature(myConfig)],
      inject: [myConfig.KEY],
      useFactory: async (
        appConfig: MyAppOptionsInterface,
      ): Promise<LoggerOptionsInterface> => appConfig.logger
  ]
});
export class App {}

Transports

If you define the transport to be used, it means that any method that you call from LoggerService will also send the details of the log to the transport defined (at the moment we are only working with Sentry as external transport).

You can easily create your own custom transports by developing a class that meets the interface.

// ...
import { LoggerModule, LoggerSentryTransport } from '@concepta/nestjs-logger';

@Module({
  imports: [
    LoggerModule.register({
      transports: [LoggerSentryTransport],
    })
  ]
});
export class App {}

Using LoggerService

After importing the module with the proper configurations, you are all set to start using the LoggerService as an injected service.

Setup Globally

It is a good practice to also inform nest to use the new Logger internally overwrite the default Logger in your bootstrap.

// ...
import { LoggerService } from '@concepta/nestjs-logger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // get reference of LoggerService From LoggerModule
  const customLoggerService = app.get(LoggerService);

  // this is to inform that this logger will be used globally
  // and it will be used once you create a new Logger()
  app.useLogger(customLoggerService);

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

Now any time you call a method from Logger class from @nestjs/common will be calling the same method from LoggerService from @concepta/nestjs-logger

Injection

You should be able to use the LoggerService by injecting the class, or creating a new instance of Logger.

import { Logger, Injectable, Inject } from '@nestjs/common';
import { LoggerService } from '@concepta/nestjs-logger';

@Injectable()
class MyService {
  constructor(@Inject(LoggerService) private loggerService: LoggerService) {}

  doSomething() {
    this.loggerService.log('Doing something...');
  }
}

Manual Instantiation

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

@Injectable()
class MyService {
  private readonly logger = new Logger(MyService.name);

  doSomething() {
    this.logger.log('Doing something...');
  }
}
4.0.0-alpha.42

3 months ago

4.0.0-alpha.41

3 months ago

4.0.0-alpha.40

3 months ago

4.0.0-alpha.39

3 months ago

4.0.0-alpha.38

3 months ago

4.0.0-alpha.37

4 months ago

4.0.0-alpha.36

5 months ago

4.0.0-alpha.35

5 months ago

4.0.0-alpha.34

5 months ago

4.0.0-alpha.31

6 months ago

4.0.0-alpha.30

7 months ago

4.0.0-alpha.33

6 months ago

4.0.0-alpha.32

6 months ago

4.0.0-alpha.28

7 months ago

4.0.0-alpha.27

8 months ago

4.0.0-alpha.26

8 months ago

4.0.0-alpha.25

9 months ago

4.0.0-alpha.29

7 months ago

4.0.0-alpha.24

9 months ago

4.0.0-alpha.23

1 year ago

4.0.0-alpha.22

2 years ago

4.0.0-alpha.19

2 years ago

4.0.0-alpha.20

2 years ago

4.0.0-alpha.21

2 years ago

4.0.0-alpha.17

2 years ago

4.0.0-alpha.16

2 years ago

4.0.0-alpha.15

2 years ago

4.0.0-alpha.14

2 years ago

4.0.0-alpha.18

2 years ago

4.0.0-alpha.13

2 years ago

4.0.0-alpha.12

2 years ago

4.0.0-alpha.11

2 years ago

4.0.0-alpha.9

2 years ago

4.0.0-alpha.10

2 years ago

4.0.0-alpha.8

2 years ago

4.0.0-alpha.7

2 years ago

4.0.0-alpha.6

2 years ago