Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context is lost when logging errors #473

Closed
floretan-ebot7 opened this issue May 6, 2022 · 15 comments · Fixed by nestjs/nest#10531
Closed

Context is lost when logging errors #473

floretan-ebot7 opened this issue May 6, 2022 · 15 comments · Fixed by nestjs/nest#10531
Labels
question Further information is requested slate

Comments

@floretan-ebot7
Copy link

While debugging while certain log entries were missing the context property, I found out that it seems that the context is lost when calling the .error() method. Here is some example code:

import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class MyService {
  private logger = new Logger(this.constructor.name);

  constructor() {
    this.logger.log('this is a test log')
    this.logger.error('this is a test error')
  }
}

Which gives this output with the json formatter:

{"context":"MyService","label":"application-platform-latency-tester","level":"info","message":"this is a test log"}
{"label":"application-platform-latency-tester","level":"error","message":"this is a test error","stack":["MyService"]}

As you can see, the first log entry is fine. The second log entry with the error is putting the context in the stack property.

With the same code, the default Nest logger keeps the context in the right place:

[Nest] 82543  - 06/05/2022, 17:37:30     LOG [MyService] this is a test log
[Nest] 82543  - 06/05/2022, 17:37:30   ERROR [MyService] this is a test error
@faithfulojebiyi
Copy link
Contributor

Your logger.error takes three arguments
Logger.error(message: any, stack?: string, context?: string),
You only passed the message to add your stack and context pass in the next arguments

You should have something like this.logger.error('a test error', error.stack, MyService.name)

@gremo
Copy link
Owner

gremo commented May 12, 2022

I'm sorry for the very late response, I'm quite busy recently with my personal and working life. I'll try to follow more this library that, honestly, went so far and has been used by many. Didn't expected that 😄

@faithfulojebiyi you seems more on the point than me, can i close this?

@gremo gremo added waiting for feedback Waiting for user feedback question Further information is requested and removed waiting for feedback Waiting for user feedback labels May 12, 2022
@faithfulojebiyi
Copy link
Contributor

faithfulojebiyi commented May 12, 2022

@gremo we could update the docs to help others out. if I also notice any other irregularities I'd let you know. let's be sure it fixed @floretan-ebot7 issue then you can close it

@gremo
Copy link
Owner

gremo commented May 12, 2022

@faithfulojebiyi provide a PR to clarify the docs then?

@faithfulojebiyi
Copy link
Contributor

@gremo PR done

@xunziheng
Copy link

Your logger.error takes three arguments Logger.error(message: any, stack?: string, context?: string), You only passed the message to add your stack and context pass in the next arguments

You should have something like this.logger.error('a test error', error.stack, MyService.name)

But he passed in the context parameter through the constructor when new Logger(), doesn't it work?

@abrindam
Copy link

@xunziheng is correct. This is a bug. The cause of this is that the NestJS Logger class is delegating the error() message to WinstonLogger, and in the case that a stack trace is not provided, it will invoke error() with only two arguments (message, context).

This is supported by the type definition (stack is an optional parameter) and should work. However, WinstonLogger is erroneously treating this invocation as (message, stack) as opposed to (message, context), so the context winds up in the stack trace instead.

In the case that WinstonLogger.error() is invoked with two arguments, the second argument should be treated as context and not as stack. This matches the expectation that NestJS has of its loggers.

@abrindam
Copy link

I should note that this is all caused by the fact that Logger.error(message: any, stack?: string, context?: string) is ambiguous at invocation time due the presence of multiple optional parameters of the same type (string). This is not nest-winston's fault; they are merely following the interface nest itself designed.

In fact, the current behavior is quite defensible given that in most languages, if you have multiple optional parameters, it is the last one that gets omitted first - this would result in (message, stack) consistent with the current implementation.

The problem is that nest itself, in what I would call a questionable decision, has decided that invoking this method with (string, string) means (message, context), not (message, stack). As much as I question this decision, I think this project must follow the lead here and change it's implementation.

@faithfulojebiyi
Copy link
Contributor

You can check the docs https://github.com/gremo/nest-winston#logger-methods usage is explained there

@abrindam
Copy link

Opened a PR with a fix: #513

Since things were already ambiguous, the fix has its downsides. Feel free to accept or reject.

@abrindam
Copy link

@faithfulojebiyi I have absolutely read the documentation if it wasn't already clear. The issue isn't the documentation, it is that when you implement a logger that works with Nest, you need to follow Nest's rules. And you're not. It is totally legitimate usage to do that the OP has done, and nothing the documentation says will change that!

To be perfectly honest, I spent a good deal of time writing up a carefully considered explanation of what is wrong here and why it is wrong, and it is concerning to receive what basically amounts to a pre-recorded reply of "please read the documentation."

@gremo
Copy link
Owner

gremo commented Jun 29, 2022

@abrindam I think we should go for the Nest-like behavior, that is following that rules:

... then:

  • We should release 2.0 because this is a BC
  • We should add small section "Upgrade" in the documentation, to explain hot to move to 2.x because this BC

Feel free to upgrade #513 to reflect this changes, thanks!

@invakid404
Copy link

I can confirm that @ChrisiPK's pull request fixed this for me! It works fine now in NestJS 9.3.1 🥳

@github-actions
Copy link
Contributor

github-actions bot commented Jul 6, 2023

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the slate label Jul 6, 2023
@github-actions
Copy link
Contributor

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested slate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants