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

feat: add threshold to always log fatal logs #4707

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Add protocol for custom screenName for UIViewControllers (#4646)
- Add threshold to always log fatal logs (#4707)

## 8.43.1-beta.0

Expand Down
13 changes: 12 additions & 1 deletion Sources/Swift/Tools/SentryLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class SentryLog: NSObject {

static private(set) var isDebug = true
static private(set) var diagnosticLevel = SentryLevel.error

/**
* Threshold log level to always log, regardless of the current configuration
*/
static let alwaysLevel = SentryLevel.fatal
private static var logOutput = SentryLogOutput()
private static var logConfigureLock = NSLock()

Expand All @@ -30,7 +35,13 @@ class SentryLog: NSObject {
*/
@objc
static func willLog(atLevel level: SentryLevel) -> Bool {
return isDebug && level != .none && level.rawValue >= diagnosticLevel.rawValue
if level == .none {
return false
}
if level.rawValue >= alwaysLevel.rawValue {
return true
}
philprime marked this conversation as resolved.
Show resolved Hide resolved
return isDebug && level.rawValue >= diagnosticLevel.rawValue
}

#if TEST || TESTCI
Expand Down
24 changes: 14 additions & 10 deletions Tests/SentryTests/Helper/SentryLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ class SentryLogTests: XCTestCase {
SentryLog.log(message: "0", andLevel: SentryLevel.error)
}

func testConfigureWithoutDebug_PrintsNothing() {
func testConfigureWithoutDebug_PrintsOnlyAlwaysThreshold() {
// -- Arrange --
let logOutput = TestLogOutput()
SentryLog.setLogOutput(logOutput)


// -- Act --
SentryLog.configure(false, diagnosticLevel: SentryLevel.none)
SentryLog.log(message: "0", andLevel: SentryLevel.fatal)
SentryLog.log(message: "0", andLevel: SentryLevel.error)
SentryLog.log(message: "0", andLevel: SentryLevel.warning)
SentryLog.log(message: "0", andLevel: SentryLevel.info)
SentryLog.log(message: "0", andLevel: SentryLevel.debug)
SentryLog.log(message: "0", andLevel: SentryLevel.none)

XCTAssertEqual(0, logOutput.loggedMessages.count)
SentryLog.log(message: "fatal", andLevel: SentryLevel.fatal)
SentryLog.log(message: "error", andLevel: SentryLevel.error)
SentryLog.log(message: "warning", andLevel: SentryLevel.warning)
SentryLog.log(message: "info", andLevel: SentryLevel.info)
SentryLog.log(message: "debug", andLevel: SentryLevel.debug)
SentryLog.log(message: "none", andLevel: SentryLevel.none)

// -- Assert --
XCTAssertEqual(1, logOutput.loggedMessages.count)
XCTAssertEqual("[Sentry] [fatal] fatal", logOutput.loggedMessages.first)
}

func testLevelNone_PrintsEverythingExceptNone() {
Expand Down
Loading