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

@einsenundnullen/better-nestjs-logger

v1.8.0

Published

Better logger for NestJS, with json and no hassle

Downloads

210

Readme

@einsenundnullen/better-nestjs-logger

This is just a simple logger for NestJS. It prints to console and can output plain strings and structured JSON.

Why is it better than the default logger?

Compared to the classic console.log statement it is not possible to pass additional data to the default NestJS logger instance. Better logger aims to provide a more coherent logging experience while still being able to print nice log lines for local development as well as JSON output for cloud environments.

BetterLogger focuses on less configuration and more convention and therefore allows only minimal configuration compared to other logging libraries.

Features

  • Provides simple logs for local development and JSON output if needed
  • Provides Request Logging out of the box
  • Can log any kind of data (even circular objects like express.Request)
  • Easy to set up

Limitations

  • Only console logging

Getting started

To get started quickly follow these steps:

1. Install the logger package

npm i @einsenundnullen/better-nestjs-logger

2. Configure BetterLoggerModule

import { BetterLoggerModule } from '@einsenundnullen/better-nestjs-logger';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    BetterLoggerModule.forRoot({
      // Enable JSON output
      json: true,
      // Set desired log levels
      logLevel: ['log', 'debug', 'error', 'verbose', 'warn'],
      // Set options for the request middleware
      requestMiddleware: {
        // enable header logging (off by default)
        headers: true,
        // set headers that should be redacted
        redactedHeaders: ['authorization', 'cookie'],
      },
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

3. Override the default Logger provider

import { BetterLogger } from '@einsenundnullen/better-nestjs-logger';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

const bootstrap = async () => {
  const app = await NestFactory.create(AppModule, {
    // Buffer logs ensures all logs get sent to the correct logger
    bufferLogs: true,
  });

  app.useLogger(app.get(BetterLogger));

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

4. Log all the stuff!

The logger is imported the same way as the NestJS logger...

// The logger name will be properly propagated (yay!)
private readonly logger = new Logger(MyService.name);

... but can now handle all kinds of log data

this.logger.verbose('Something happened!');

this.logger.log('Something happened!', {
  usefulvalue: 'i am useful information',
});

this.logger.debug('Something happened!', {
  usefulvalue: 'i am useful information',
});

this.logger.error(
  'Something bad happened!',
  {
    usefulvalue: 'i am useful information',
  },
  some_error,
);

this.logger.warn(
  'Something happened!',
  {
    usefulvalue: 'i am useful information',
  },
  ['some item', 'another item'],
);

5. Profit

Leave a like or an issue if you enjoy this package!