log4js-node module for Nest
$ npm i --save @nsboot/log4js log4js
- import
LoggerModule
in your root module
import { Module } from '@nestjs/common';
import { LoggerModule } from '@nsboot/log4js';
@Module({
imports: [
LoggerModule.forRootAsync({
path: 'logs',
level: 'info',
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
- use
LoggerService
in your service
import { Injectable } from '@nestjs/common';
import { LoggerService, InjectLogger, ILogger, Logger } from '@nsboot/log4js';
@Injectable()
export class AppService {
private logger: Logger;
//must use @InjectLogger() to inject logger
constructor(@InjectLogger() private readonly iLogger: ILogger) {
this.logger = this.iLogger.getLogger(AppService.name);
}
getHello(): string {
this.logger.info('getHello');
return 'Hello World!';
}
}
- or use
@Log4j()
decorator in your service
import { Injectable } from '@nestjs/common';
import { Logger, Log4j } from '@nsboot/logger';
@Injectable()
export class AppService {
@Log4j()
private logger: Logger;
getHello(): string {
this.logger.info('getHello');
return 'Hello World!';
}
}
Note:
@Log4j()
decorator will injectLogger
instance to your service, so you can usethis.logger
to log. If you want to use@InjectLogger()
decorator, you must usethis.iLogger.getLogger()
to getLogger
instance.
Config
interface Config {
path?: string;
level?: string;
// file name
name?: string;
// console show log
console?: boolean;
}
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
@nsboot/logger is MIT licensed.