npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@concepta/nestjs-logger

v4.0.0-alpha.42

Published

Rockets NestJS Logger

Downloads

108

Readme

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...');
  }
}