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

@wiredcraft/nestjs-bunyan-logger

v3.0.0

Published

A logger module for [Nestjs](https://github.com/nestjs/nest), built on top of [node-bunyan](https://github.com/trentm/node-bunyan).

Downloads

151

Readme

Description

A logger module for Nestjs, built on top of node-bunyan.

Features

  • A gloabl bunyan logger provider can be used in the controllers/services.
  • Automatically log request/response with request-id, timestamp, status-code, etc.

Usage

Installation

Yarn

yarn add @wiredcraft/nestjs-bunyan-logger

NPM

npm install @wiredcraft/nestjs-bunyan-logger --save

Integration

  1. Import LoggerModule in the root App module, this provides initialized bunyan logger instance that is available to other modules by injection.
import { Module } from '@nestjs/common';
import { LoggerConfig, LoggerModule } from '@wiredcraft/nestjs-bunyan-logger';
import configuration from './src/configuration';

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
    }),
    LoggerModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        return configService.get<LoggerConfig>('logger', { infer: true });
      },
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

A sample configuration file is as below,

export default () => ({
  logger: {
    name: 'awesome-app',
    streamType: 'FILE' | 'STDOUT',
    path: './logs/app.log', // only available for `FILE` streamType
    excludeReqPath: '/health', // the path that you want to skip logging
  },
});
  1. Inject the logger instance to your service and use it as you want.
import { Injectable } from '@nestjs/common';
import { Logger, Bunyan } from '@wiredcraf/nestjs-bunyan-logger';

@Injectable()
export class CatService {
  constructor(@Logger() private logger: Bunyan) {}
}

Transformers Option

image

transformers is a config option that could allow the user to specify a list of customized transform rules to apply to each log entry. The current library supports three types of transformers: constant, clone and map.

Constant

constant type allows the user to copy a object type constant directly to the log entry. An example is as follows:

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers: [{ constant: { customField: '123' } }],
};

logger.info({ originalField: 'abc' });

// The above will generate the following log entry:
//  {
//    originalField: 'abc',
//    customField: '123'
//  }

Clone

clone type allows the user to copy value from one fieldname to the specified fieldname with the following rules:

  • The original field is not removed from the log entry
  • Only one embed level is allowed in new Fieldname specification

An example is as follows:

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers: [{ clone: { originalField: 'newField' } }],
};

logger.info({ originalField: 'abc' });

// The above will generate the following log entry:
//  {
//    originalField: 'abc',
//    newField: 'abc'
//  }

An one embed level example is as follows:

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers: [{ clone: { originalField: 'newParent.newField' } }],
};

logger.info({ originalField: 'abc' });

// The above will generate the following log entry:
//  {
//    originalField: 'abc',
//    newParent: {
//      {
//        newField: 'abc'
//      }
//    }
//  }

Map

map type allows the user to apply a map value or a map function to the target field and replace the field value with the mapped value with the following rules:

  • only one embed level is allowed for original field name specification

An example of simple map value is as follows:

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers: [{ map: { originalField: 'new-value' } }],
};

logger.info({ originalField: 'abc' });

// The above will generate the following log entry:
//  {
//    originalField: 'new-value',
//  }

An example of map function is as follows:

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers: [
    { map: { originalField: (oldValue) => oldValue + 'transformed' } },
  ],
};

logger.info({ originalField: 'abc' });

// The above will generate the following log entry:
//  {
//    originalField: 'abctransformed',
//  }

An one embed level example is as follows:

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers: [
    { map: { ['originalParentField.originalField']: 'new-value' } },
  ],
};

logger.info({
  originalParentField: {
    originalField: 'abc',
  },
});

// The above will generate the following log entry:
// {
//   originalParentField: {
//    originalField: 'new-value',
//   },
// }

Multiple transformers

Multiple transformers are applied in the order as they are specified in the transformers option. An example is as follows:

const transformers = [
  {
    constant: { customField: 'custom-value' },
  },
  {
    clone: { originalField: 'backupField' },
  },
  {
    map: { originalField: (oldValue) => oldValue + 'transformed' },
  },
];

const options: LoggerConfig = {
  name: 'test-app',
  streamType: 'STDOUT',
  transformers,
};

logger.info({ originalField: 'original-value' });

// The above will generate the following log entry:
// {
//   originalField: 'original-value-transformed',
//   backupField: 'original-value',
//   customField: 'custom-value'
// }

Development

Installation

$ npm install

Publish

$ npm version major|minor|patch
$ npm publish

License

MIT licensed.