@we-are-singular/logger
v2.1.3
Published
A simple pinojs wrapper
Downloads
899
Readme
@we-are-singular/logger
This package provides a shared ESLint configuration for Singular projects.
Installation
npm install --save-dev @we-are-singular/loggerUsage
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
Using NestJSLoggerClass (recommended)
override default logger:
import { NestJSLoggerClass, LoggerFactory } from "@we-are-singular/logger"
// Using direct instantiation
const app = await NestFactory.createApplicationContext(AppModule, {
logger: new NestJSLoggerClass().withModule("AppModule").forRoot(),
})
// Using LoggerFactory
const app = await NestFactory.createApplicationContext(AppModule, {
logger: LoggerFactory.forNestJS("AppModule").forRoot(),
})auto context from module provider:
// use forFeature() In other modules
@Module({
imports: [],
controllers: [],
providers: [
//
NestJSLoggerClass.forFeature("Services"),
],
exports: [
//
NestJSLoggerClass,
],
})
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: NestJSLoggerClass
constructor(
//
@Inject(NestJSLoggerClass) logger: NestJSLoggerClass
) {
this.logger = logger.withContext(this.constructor.name.toString())
}
}Using LoggerService (deprecated)
Note:
LoggerServiceis deprecated and will be removed in a future version. Please useNestJSLoggerClassinstead.
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())
}
}Fastify
Using FastifyLoggerClass
The FastifyLoggerClass implements the FastifyBaseLogger interface and can be used directly with Fastify:
import fastify from "fastify"
import { FastifyLoggerClass, LoggerFactory } from "@we-are-singular/logger"
// Using direct instantiation with app and module
const app = fastify({
logger: new FastifyLoggerClass("MyApp", "api"),
})
// Using LoggerFactory
const app = fastify({
logger: LoggerFactory.forFastify("MyApp", "api"),
})
// Just module (app will be empty)
const app = fastify({
logger: new FastifyLoggerClass(undefined, "api"),
})This replaces the need for custom logger creation like:
// Old way (no longer needed)
function createFastifyLogger(): FastifyBaseLogger {
const apiLogger = logger.fork().setModule("api")
return {
level: "info",
info: apiLogger.info.bind(apiLogger),
error: apiLogger.error.bind(apiLogger),
debug: apiLogger.debug.bind(apiLogger),
fatal: apiLogger.fatal.bind(apiLogger),
warn: apiLogger.warn.bind(apiLogger),
trace: apiLogger.trace.bind(apiLogger),
silent: apiLogger.silent?.bind(apiLogger) || (() => {}),
child: () => createFastifyLogger(),
}
}