Skip to content

a vanilla flavoured wrapper around pino

License

Notifications You must be signed in to change notification settings

we-are-singular/logger

Repository files navigation

@we-are-singular/logger

npm version

This package provides a shared ESLint configuration for Singular projects.

Installation

npm install --save-dev @we-are-singular/logger

Usage

import { LoggerClass } from "@we-are-singular/logger"

const logger = new LoggerClass("MyApp", "Module", "Context")
logger.info("Hello, world!")
logger.error("Something went wrong!")
logger.withModule("AnotherModule").info("Hello, world!")

NestJS

override default logger:

import { LoggerService } from "@we-are-singular/logger"

// use forRoot() In main.ts
const app = await NestFactory.createApplicationContext(AppModule, {
  logger: new LoggerService().withModule("AppModule").forRoot(),
})

auto context from module provider:

// use forFeature() In other modules
@Module({
  imports: [],
  controllers: [],
  providers: [
    //
    LoggerService.forFeature("Services"),
  ],
  exports: [
    //
    LoggerService,
  ],
})
export class ServicesModule {}

auto context:

// as a base class, adding a logger to all classes with a context of the class name
@Injectable()
export abstract class BaseClass {
  readonly logger: LoggerService
  constructor(
    //
    @Inject(LoggerService) logger: LoggerService
  ) {
    this.logger = logger.withContext(this.constructor.name.toString())
  }
}