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

@manogel/nestjs-log-trail

v0.1.4

Published

Custom logger for NestJS

Downloads

2

Readme

Motivation

For a long time, I was frustrated with errors in production applications. My time has always invested a lot of working time trying to understand why the frontend was not behaving correctly with unknown errors in the backend and at the same time many problems persist in the backend and go unnoticed, either because it was developed by a junior team or the code review failed.

This package implements a custom logger to allow quick and easy detection of problems, mainly in production applications.

Another motivator is to allow access to logs triggered by the system and use this information to save in a database or even alert other systems such as Discord, Slack, etc.

Features

  • Log every request/response errors automatically
  • Log every request/response if configured
  • Log in JSON or stringified JSON
  • Bind extra informations anywhere application layer for more details in your logs
  • Setup a logger listener for more control with your logs

Index

Installation

npm i @manogel/nestjs-log-trail

or

yarn add @manogel/nestjs-log-trail

Getting Started

Default object configuration

AppLoggerModule.register({
    applyForRoutes: [{ path: '*', method: RequestMethod.ALL }],
    convertLogObjToString: false,
    setLoggerListener: undefined,
    logAllRequests: false,
    logOnlyErrorRequests: true,
    modifyErrorPrototype: true,
  })

Default configuration with register

Update app.module.ts with:

import { AppLoggerModule } from '@manogel/nestjs-log-trail';

@Module({
  imports: [
    AppLoggerModule.register({})
  ],
})

Default configuration with registerAsync

Update app.module.ts with:

import { AppLoggerModule } from '@manogel/nestjs-log-trail';

@Module({
  imports: [
    AppLoggerModule.registerAsync({
      imports: [],
      inject: [],
      useFactory() {
        return {};
      },
    }),
  ],
})

Example: Saving logs to the database

See source code in src (NestJs, Prisma with SQLite)

@Module({
  imports: [
    PrismaModule,
    AppLoggerModule.registerAsync({
      useFactory: (prismaService: PrismaService) => {
        return {
          convertLogObjToString: false,
          async setLoggerListener(data) {
            await prismaService.applicationLogger.create({
              data: {
                level: data.level,
                msg: data.msg,
                context: data.context,
                date: data.date,
                req: JSON.stringify(data.req),
                res: JSON.stringify(data.res),
                extra: JSON.stringify(data.extra),
              },
              select: { id: true },
            });
          },
        };
      },
      inject: [PrismaService],
      imports: [PrismaModule],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Common questions

Why not pay for a monitoring system instead of this package?

I've always had problems with third-party tools for tracking issues. Either because I didn't have all the necessary information or difficulty using it. So I made my own logger implementation for total control of informations.

What is the modifyErrorPrototype parameter used for?

Is an parameter enable by default to allow convert Error class in stringified object.

if (!('toJSON' in Error.prototype))
  Object.defineProperty(Error.prototype, 'toJSON', {
    value: function () {
      const alt = {};

      Object.getOwnPropertyNames(this).forEach(function (key) {
        alt[key] = this[key];
      }, this);

      return alt;
    },
    configurable: true,
    writable: true,
  });

For more details: see stackoverflow

I need your help

I need your help for collaborate and maintain this project 😊. Every help is welcome.