@skeynetwork/nlogger
v3.1.9
Published
logger
Downloads
1
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'],
},
}