[PR #3405] [MERGED] Log non-strings into log file like console.log does #3961

Closed
opened 2026-04-25 00:17:44 +02:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/advplyr/audiobookshelf/pull/3405
Author: @mikiher
Created: 9/11/2024
Status: Merged
Merged: 9/11/2024
Merged by: @advplyr

Base: masterHead: logger-fixes


📝 Commits (3)

  • 682a99d Log non-strings into log file like console.log does
  • 220f7ef Resolve some weird unrelated flakiness in BookFinder test
  • 03ff5d8 Disregard socketListener.level if level >= FATAL

📊 Changes

3 files changed (+437 additions, -172 deletions)

View changed files

📝 server/Logger.js (+33 -30)
test/server/Logger.test.js (+285 -0)
📝 test/server/finders/BookFinder.test.js (+119 -142)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/advplyr/audiobookshelf/pull/3405 **Author:** [@mikiher](https://github.com/mikiher) **Created:** 9/11/2024 **Status:** ✅ Merged **Merged:** 9/11/2024 **Merged by:** [@advplyr](https://github.com/advplyr) **Base:** `master` ← **Head:** `logger-fixes` --- ### 📝 Commits (3) - [`682a99d`](https://github.com/advplyr/audiobookshelf/commit/682a99dd439230f28b3be990026f4e4d4e447853) Log non-strings into log file like console.log does - [`220f7ef`](https://github.com/advplyr/audiobookshelf/commit/220f7ef7cdecd32146a41c707d2e61602b09b645) Resolve some weird unrelated flakiness in BookFinder test - [`03ff5d8`](https://github.com/advplyr/audiobookshelf/commit/03ff5d8ae1467324be7a3aab517184c2b4ef74d9) Disregard socketListener.level if level >= FATAL ### 📊 Changes **3 files changed** (+437 additions, -172 deletions) <details> <summary>View changed files</summary> 📝 `server/Logger.js` (+33 -30) ➕ `test/server/Logger.test.js` (+285 -0) 📝 `test/server/finders/BookFinder.test.js` (+119 -142) </details> ### 📄 Description 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: ```js 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-04-25 00:17:44 +02:00
adam closed this issue 2026-04-25 00:17:44 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#3961