question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] PinoLogger: unable to assign extra fields out of request scope // trying to assign fields from AuthGuard

See original GitHub issue

[x] I’ve read the docs of nestjs-pino

[x] I’ve read the docs of pino

[x] I couldn’t find the same question about nestjs-pino

Question Hello guys, we require to extend our logs with user data which get set to the REQ object by using a nestjs auth guard. Sadly those are not getting into the logs. I’m now trying to set the fields specifically using the “assign” method. But as it seems (and which makes sense) it is not possible due to the auth guard not beeing request scoped. How can we get our request context based information into the logs in a uniform way (we need to provide this from a libary, ideally in form of a middleware) because we require this in a lot of different microservices. I’ve tried writing a request scoped middleware but it gives the same error:

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Response, NextFunction } from 'express';
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';
import { UserRequest } from '@triluxds/tds-lp-nest-utilities';

@Injectable({ scope: Scope.REQUEST })
export class LoggerContextMiddleware implements NestMiddleware {
  constructor(
    @InjectPinoLogger(LoggerContextMiddleware.name)
    private readonly logger: PinoLogger,
  ) {}
  use(req: UserRequest, res: Response, next: NextFunction) {
    this.logger.assign({ userId: req.user?.userId });
    this.logger.assign({ customerId: req.user?.customerId });
    this.logger.assign({ clientId: req.user?.clientId });
    next();
  }
}

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerContextMiddleware).forRoutes('*');
  }
}

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
schealexcommented, Jul 6, 2022

i’ve opened a bug report so I’m closing this question

0reactions
iamoleggacommented, Jul 6, 2022

check the order of modules then.If you believe this is a bug feel free to open a bug report with minimal example

Read more comments on GitHub >

github_iconTop Results From Across the Web

[FEATURE REQUEST] Adding Additional Request Scoped ...
I am getting PinoLogger: unable to assign extra fields out of request scope with Pubsub.
Read more >
How to use nestjs Logging service - Stack Overflow
I am trying to show a sample custom logger implementation and how it can be injected to the Nestjs framework. I understand that...
Read more >
@useguards(authguard('local')) - You.com - You.com
The problem is how to ensure that validation of the required field before run guard ... to solve it, I add some validation...
Read more >
How to wait for an API call in canActivate auth guard in angular
Please post your questions in the comment section and I will be happy to answer your questions. Subscribe for more useful videos just...
Read more >
07 - Create Authorization Route Guard - NestJS - YouTube
In this tutorial, we'll learn how to use the JSON Web Token we created in the previous tutorial to rejoin a poll that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found