JSPM

@skeynetwork/nlogger

3.1.9
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q47810F
  • License MIT

logger

Package Exports

  • @skeynetwork/nlogger
  • @skeynetwork/nlogger/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@skeynetwork/nlogger) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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