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

nest-request-postgres-logger

v3.0.7

Published

nest-request-postgres-logger

Downloads

37

Readme

Nest request postgres logger

Features:

  • Logs request data to terminal
  • Saves requests and logs to postgres db
  • You can hide secret properties like passwords etc.
  • Choose request headers you want to display and save
  • Saved logs are matched with their request
  • You can attach user id and custom data to request logs
  • Uses its own schema in postgres

Requirements:

  • nestjs-cls
  • @mikro-orm
  • postgres database

Minimal setup

Install the library with peer dependencies

npm install nestjs-cls @mikro-orm/core @mikro-orm/postgresql @mikro-orm/migrations nest-request-postgres-logger

Add nest logger module

// app.module.ts

import { Module } from '@nestjs/common'
import { ClsModule } from 'nestjs-cls'
import { NLoggerModule } from 'nest-request-postgres-logger'

@Module({
  imports: [
    ClsModule.forRoot({ global: true, middleware: { mount: true } }),
    NLoggerModule.forRoot({
      dbConfig: {
        host: 'localhost',
        port: 5432,
        dbName: 'postgres',
        user: 'postgres',
        password: 'password',
      },
    }),
  ],
})
export class AppModule {}

Replace default nest logger

// main.ts

import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { NLogger } from 'nest-request-postgres-logger'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.useLogger(app.get(NLogger))

  await app.listen(3000)
}

bootstrap()

Configuration

export interface NLoggerOptions {
  /**
   * Mikro orm connection config
   * https://mikro-orm.io/api/5.6/core/interface/MikroORMOptions
   *
   * Some properties are overridden by logger
   */
  dbConfig: DbConfig

  /**
   * Config to syslog transport to papertrail
   */
  papertrailConfig?: PapertrailConfig

  /**
   * Whether to mount middleware automatically
   *
   * Defaults to true
   */
  mountMiddleware?: boolean

  /**
   * List of request headers to log and save
   *
   * Defaults to ['user-agent', 'content-type']
   */
  headers?: string[]

  /**
   * List of property keys to hide from request body and query
   *
   * Defaults to []
   */
  hiddenProperties?: string[]

  /**
   * Result of this function will be saved with request as userId
   */
  userIdExtractor?: (req: Request, cls: ClsService) => string | null

  /**
   * Result of this function will be saved with request customData
   */
  customDataExtractor?: (req: Request, cls: ClsService) => Record<string, any> | null

  /**
   * Skip logging messages from these contexts
   *
   * Defaults to [
   *   'RouterExplorer',
   *   'InstanceLoader',
   *   'NestFactory',
   *   'NestApplication',
   *   'ClsModule',
   *   'RoutesResolver'
   * ]
   */
  skipContexts?: string[]

  /**
   * Optionally substitute 'Nest' and pid prefix with custom string
   */
  logPrefix?: string
}

Using with mikro-orm

When generating migrations with mikro-orm it will probably try to remove nlogger schema. To prevent this use this option in mikro-orm configuration

export const options: MikroOrmModuleOptions = {
  // ...
  schemaGenerator: {
    // ...
    ignoreSchema: ['nlogger'],
  },
}