Code Monkey home page Code Monkey logo

Comments (18)

iamolegga avatar iamolegga commented on May 13, 2024 2

@dguyonvarch feel free to open pr for this

from nestjs-pino.

dguyonvarch avatar dguyonvarch commented on May 13, 2024 2

No sooner said than done !
#441

With this PR we can now do :

// logger.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { Logger, Params, PARAMS_PROVIDER_TOKEN, PinoLogger } from 'nestjs-pino';

@Injectable()
export class LoggerService extends Logger {
    private aProperty: string;
    
    constructor(
        pinoLogger: PinoLogger,
        @Inject(PARAMS_PROVIDER_TOKEN) params: Params,
        aProperty: string
    ) {
        super(pinoLogger, params);
        this.aProperty = aProperty
    }

    aMethod(): void {
        ...
    }
}

and

// logger.module.ts
import { Module } from "@nestjs/common";
import { LoggerModule  } from "nestjs-pino";
import { LoggerService } from "./logger.service";

@Module({
  exports: [LoggerService],
  providers: [LoggerService],
  imports: [
   LoggerModule.forRoot()
  ],
})
export class LoggerModule {}

from nestjs-pino.

jdt6 avatar jdt6 commented on May 13, 2024 2

until PR gets merged, @iamolegga, or @dguyonvarch what is the work around for adding a wrapper around the Logger?

from nestjs-pino.

iamolegga avatar iamolegga commented on May 13, 2024 1

Thanks, I'll try to find time for this in a week

from nestjs-pino.

AckerApple avatar AckerApple commented on May 13, 2024 1

I arrived at this issue needing to override pino and extend it's functionality.... I have since solved my issue and here is my working override code that makes the root pino give me override access:

TypeScript

import pino from 'pino'
const myPino = pino()
const orgChildMethod = myPino.child
myPino.child = function(this: any, details: any) {
  const newPino = orgChildMethod.call(this, details)
  newPino.info = function(logMeta: any): void {
    logger.info({...logMeta, ...details}) // our custom override
  }
  newPino.warn = function(logMeta: any): void {
    logger.warning({...logMeta, ...details}) // our custom override
  }
  return newPino
}

JavaScript

const myPino = require('pino')()
const orgChildMethod = myPino.child
myPino.child = function(details) {
  const newPino = orgChildMethod.call(this, details)
  newPino.info = function(logMeta) {
    logger.info({...logMeta, ...details}) // our custom override
  }
  newPino.warn = function(logMeta) {
    logger.warning({...logMeta, ...details}) // our custom override
  }
  return newPino
}

from nestjs-pino.

AckerApple avatar AckerApple commented on May 13, 2024 1

@delai I have copy, pasted, and edited the clean solution we have been running on several projects.

Good luck reading out the details, I just quick and dirty got you the bulk of our solution

import { Module, Logger } from '@nestjs/common'
import { LoggerModule } from 'nestjs-pino'
import pino from 'pino'

@Module({
  controllers: [],
  providers: [ Logger ],
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        enabled: true,
        logger: getOurPino(logger), // see function below for details
      }
    })
  ],
})
export default class AppModule {
}

/** Pino logger functionality override to report to us instead of console
 * We use pino to ensure request.id in every log.
 * It has a child recursive nature to it that makes overrides a bit complex
*/
export function getOurPino(
   logger: any // We want Pino to call something else for every log (our internal logging tool)
): pino.Logger {
  const myPino = pino()
  const orgChildMethod = myPino.child
  myPino.child = function(this: any, details: any) {
    const newPino = orgChildMethod.call(this, details)

    /* customs overrides */
      newPino.info = function(logMeta: any): void {
        logger.info({...logMeta, ...details})
      }

      newPino.warn = function(logMeta: any): void {
        logger.warning({...logMeta, ...details})
      }

      newPino.error = function(logMeta: any): void {
        logger.error({...logMeta, ...details})
      }

      newPino.debug = function(logMeta: any): void {
        logger.debug({...logMeta, ...details})
      }
    /* end: custom overrides */

    return newPino
  }

  return myPino
}

from nestjs-pino.

AckerApple avatar AckerApple commented on May 13, 2024 1

@iamolegga im on v6 and seems that’s a v7 feature

I will look to check it out

from nestjs-pino.

iamolegga avatar iamolegga commented on May 13, 2024

Hi, easiest way is composition here, but if you want extend it please provide minimal repo for that without business logic as in your example above, I could take a look at it

from nestjs-pino.

alexabidri avatar alexabidri commented on May 13, 2024

Thanks @iamolegga , here is the repo where you can find the simple logger module with controller "Hello World" https://github.com/alexabidri/nestjs-pino--test

from nestjs-pino.

orzarchi avatar orzarchi commented on May 13, 2024

any updates regarding this? I'd also like to add some logic to logger.error (sending errors to Sentry)

from nestjs-pino.

iamolegga avatar iamolegga commented on May 13, 2024

@orzarchi this can be done with current implementation, see docs of this lib and https://github.com/aandrewww/pino-sentry#usage

from nestjs-pino.

dguyonvarch avatar dguyonvarch commented on May 13, 2024

This error is due to pino-params provider which is not exported by the module : https://github.com/iamolegga/nestjs-pino/blob/master/src/LoggerCoreModule.ts#L61

Any way to export it ? I would like to extend Logger too for separation of concerns logic.

from nestjs-pino.

AckerApple avatar AckerApple commented on May 13, 2024

Look in the comments @jdt6 I posted a working solution in both TypeScript and JavaScript

from nestjs-pino.

iamolegga avatar iamolegga commented on May 13, 2024

closed in #441

from nestjs-pino.

delai avatar delai commented on May 13, 2024

@AckerApple how can i use the code, run the code in every nestjs controller or service? is there any elegant way?

from nestjs-pino.

iamolegga avatar iamolegga commented on May 13, 2024

@AckerApple may I ask you why an extension of PinoLogger doesn't fit for you?

Pino logger functionality override to report to us instead of console

you can pass transports as a pinoHttp.stream field of module settings. But I personally prefer to pipe stdout to the transport in that case

from nestjs-pino.

condef5 avatar condef5 commented on May 13, 2024

@AckerApple in this part

       logger: getOurPino(logger), // see function below for details

where does this logger variable come from?

from nestjs-pino.

AckerApple avatar AckerApple commented on May 13, 2024

The short: We have our own company logger so we pass that in there.

The long: “getOurPino()” as seen above, essentially takes Pino logs and casts them to fit our needed format. So the argument “logger” is what our company passes in and one main difference is our company logger uses “warning” instead of “warn” but thats the flexibility we get with “getOurPino()”. So if you needed to cast Pino logs to something of your own format, then copy “getOurPino()” and edited it to your needs.

The data typing for logger, instead of any, should be:

logger: {
  info: (…args: any[]) => any,
  warning: (…args: any[]) => any,
  debug: (…args: any[]) => any,
  error: (…args: any[]) => any,
}

from nestjs-pino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.