iamolegga / nestjs-pino

Platform agnostic logger for NestJS based on Pino with REQUEST CONTEXT IN EVERY LOG
MIT License
1.26k stars 98 forks source link

[QUESTION] - How to use extended Logger methods in other services #1956

Closed KyleBrown-804 closed 4 months ago

KyleBrown-804 commented 4 months ago

Question Context: I figured out from a previous question how I could extend functionality from the Logger or PinoLogger from 'nestjs-pino' here: https://github.com/iamolegga/nestjs-pino/issues/391 and I can see how example of what's added in #441 should work with what I want, which is to add a new method let's call it "serviceError()". I want to add some additional logic and metadata to certain error logs and that method will then invoke super.error().

Part I'm not understanding: What I haven't figured out from also looking at the example in the readme for usage, is how I could actually access this added method "serviceError()" from the logger if in my service I am importing import { Logger } from '@nestjs/common. That logger doesn't have the added method so just trying to understand how I could use methods from an extended logger so that I can use this.logger.serviceError() in place of this.logger.error() in a service.

Please mention other relevant information such as Node.js version and Operating System.

iamolegga commented 4 months ago

NestJS built-in logger cannot be extended by design. It has only a specific amount of methods and that's it. That's why this library consists of 2 loggers. One is the idiomatic NestJS logger implementation used for substitution under the hood when using the built-in logger. The second one is used for specific features and could be extended and injected where needed.