@viplance/nestjs-logger
v0.4.4
Published
NestJS internal logging system
Maintainers
Readme
@viplance/nestjs-logger
NestJS internal logging system
Installation
- Install the package
npm i @viplance/nestjs-logger - Import the module in app.module.ts
import { LogModule } from '@viplance/nestjs-logger';
@Module({
imports: [
...,
LogModule,
]
})- Init the module (typically in main.ts)
import { LogModule } from '@viplance/nestjs-logger';
await LogModule.init(app, {
..., // some other properties
path: '/logs', // define the public URL for the log list
key: 'kjhjmi321lqq7a', // use the key to protect data from unauthorized access
});Connect a SQL or NoSQL database to store logs.
await LogModule.init(app, {
...,
database: {
type: 'mongodb',
host: 'localhost',
port: 27017,
collection: 'logs',
}
});Enable a WebSocket connection to receive the logs in real time.
await LogModule.init(app, {
...,
websocket: {
secure: true, // enable WSS
port: 81,
host: 'your-domain.name',
}
});- Use the
LogServicein case of custom logs to debug the application.
import { LogService } from '@viplance/nestjs-logger';
constructor(private logService: LogService) {}
this.logService.log('Some log information');Additional information
path,key,databaseandwebsocketproperties are optional.- The log UI could be available at
your_application_url/path?key=keyor WebSocket port - The log API could be available at
your_application_url/path/api?key=key - By default the logs will be stored in memory and deleted when the application stops.
The LogModule options:
- path?: string;
- key?: string; // access key
- join?: boolean; // merge the message duplicates
- maxRecords?: number; // max log records
- maxAge?: number; // in days
- maxSize?: number; // in megabytes
- database?: DataSourceOptions & { host?: string; port?: string; table?: string; collection?: string; };
- websocket?: { port?: number; namespace?: string; host?: string; secure?: boolean; };
The LogService methods:
- log(message: string, context?: object)
- error(message: string, context?: object)
- warn(message: string, context?: object)
- debug(message: string, context?: object)
- verbose(message: string, context?: object)
- addBreadcrumb(breadcrumb: any) - adds extra details to the logs for the current request
