Skip to content

Commit ce83fcb

Browse files
authored
Merge pull request #1186 from ahoppen/truncate-non-darwing-log-message
Truncate log message in `NonDarwinLogger` at 10.000 characters
2 parents b9af0cf + 8f74261 commit ce83fcb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Sources/LSPLogging/NonDarwinLogging.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,20 @@ public struct NonDarwinLogger: Sendable {
299299
guard level >= self.logLevel else { return }
300300
let date = Date()
301301
loggingQueue.async {
302+
// Truncate log message after 10.000 characters to avoid flooding the log with huge log messages (eg. from a
303+
// sourcekitd response). 10.000 characters was chosen because it seems to fit the result of most sourcekitd
304+
// responses that are not generated interface or global completion results (which are a lot bigger).
305+
var message = message().value.string(for: self.privacyLevel)
306+
if message.utf8.count > 10_000 {
307+
// Check for UTF-8 byte length first because that's faster since it doesn't need to count UTF-8 characters.
308+
// Truncate using `.prefix` to avoid cutting of in the middle of a UTF-8 multi-byte character.
309+
message = message.prefix(10_000) + "..."
310+
}
302311
// Start each log message with `[org.swift.sourcekit-lsp` so that it’s easy to split the log to the different messages
303312
logHandler(
304313
"""
305314
[\(subsystem):\(category)] \(level) \(dateFormatter.string(from: date))
306-
\(message().value.string(for: self.privacyLevel))
315+
\(message)
307316
---
308317
"""
309318
)

0 commit comments

Comments
 (0)