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

Log non-strings into log file like console.log does #3405

Merged
merged 3 commits into from
Sep 11, 2024

Conversation

mikiher
Copy link
Contributor

@mikiher mikiher commented Sep 11, 2024

This was inspired by #3398, in which the reporter attaches the log file, which doesn't contain a detailed logging of AggregateError.

Examining the Logger, I found that when logging to file and emiting log events to listeners, the log object is built like this:

    const logObj = {
      timestamp: this.timestamp,
      source: src,
      message: args.join(' '),
      levelName: this.getLogLevelString(level),
      level
    }

However, Array.join calls toString on each arg, which, in the case of Error args, just returns the name and message of the error, without the stack trace or any additional fields.

console.log on the other hand, uses util.inspect on non-string arguments, which returns a more detailed object representation, and also has special handling for Error objects.

I fixed this issue by mimicking how console.log builds the message.

I also made a couple of other minor changes:

  • Moved the logging logic into a single private method \#log
  • Before my changes, fatal and note always logged, regardless of this.logLevel, but file logging still checked against this.logLevel. I fixed the inconsistency (now file logging also disregards this.logLevel for fatal and note). I didn't know if I should also disregard it for log event emitting? currently left that unchanged.

In addition, I added unit tests for Logger.

@advplyr
Copy link
Owner

advplyr commented Sep 11, 2024

That makes sense. We can disregard those for the log event emitting also.

@mikiher
Copy link
Contributor Author

mikiher commented Sep 11, 2024

I also needed to resolve a seemingly unrelated flakiness in the BookFinder unit test.

Not sure exactly what happened there. I modified the test code slightly and it went away (the many changes are due to prettier formatting).

@mikiher
Copy link
Contributor Author

mikiher commented Sep 11, 2024

We can disregard those for the log event emitting also.

OK, I'll fix the code then.

@mikiher mikiher marked this pull request as ready for review September 11, 2024 19:07
@mikiher
Copy link
Contributor Author

mikiher commented Sep 11, 2024

Done. This is ready for review.

@advplyr
Copy link
Owner

advplyr commented Sep 11, 2024

Thanks!

@advplyr advplyr merged commit 703477b into advplyr:master Sep 11, 2024
5 checks passed
@mikiher mikiher deleted the logger-fixes branch September 12, 2024 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants