3.0.7 • Published 2 months ago

nest-request-postgres-logger v3.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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'],
  },
}
3.0.7

2 months ago

3.0.6

2 months ago

3.0.4

2 months ago

3.0.3

2 months ago

3.0.2

2 months ago

3.0.1

2 months ago

3.0.5

2 months ago

3.0.0

2 months ago

2.0.0

4 months ago

1.0.11

5 months ago

1.0.10

5 months ago

1.0.9

5 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.3

11 months ago