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

nestjs-logger-span

v0.2.1

Published

NestJS Logger Span

Downloads

4

Readme

NestJS Logger Span

NestJS Logger Span codecov npm package

A winston based logger library that adds the spanId field throughout the application flow.

Install

npm install winston nestjs-logger-span

How to use

In the main.ts file in the create method add the bufferLogs property.

const app = await NestFactory.create(AppModule, { bufferLogs: true });

Also in the main.ts file add useLogger.

import { LoggerServiceCustom } from 'nestjs-logger-span';

app.useLogger(app.get(LoggerServiceCustom));

The main.ts file should look similar to the one below.

import { NestFactory } from '@nestjs/core';
import { LoggerServiceCustom } from 'nestjs-logger-span';

import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useLogger(app.get(LoggerServiceCustom));
  await app.listen(3000);
}
bootstrap();

In the app.module.ts file should be imported the nestjs-logger-span module and also added the middleware to identify the entire application flow in an http request.

import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { LoggerModule, SpanMiddleware } from 'nestjs-logger-span';
import { transports, format, createLogger } from 'winston';

const configureLogger = () => {
  const consoleFormat = format.combine(format.timestamp(), format.json());
  return createLogger({
    level: process.env.DEBUG ? 'debug' : 'info',
    transports: [new transports.Console({ format: consoleFormat })],
  });
};

@Module({
  imports: [LoggerModule.forRoot(configureLogger())]
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(SpanMiddleware).forRoutes('*');
  }
}

With this library you can customize winston settings as you wish.

But there are situations where you want to track a flow of your application that does not start on an http request, so for this purpose the decorator @Span was created.

import { Span } from 'nestjs-logger-span';

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

  constructor(@Inject(MEDIA_SERVICE) private readonly mediaService: MediaService) {}

  @Span
  listener(message: string, fields: MessageFields, properties: MessageProperties) {
    this.logger.log(`listener - Message [${message}]`);
    this.mediaService.read(message);
  }
}