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

impr: Add timeIntervalSince1970 to SDK log messages #4781

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Changes from 2 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
@@ -10,6 +10,7 @@

- Add more debug logs for SentryViewHierarchy (#4780)
- Add `sample_rand` to baggage (#4751)
- Add uptime to log messages (#4781)

## Fixes

14 changes: 13 additions & 1 deletion Sources/Swift/Tools/SentryLog.swift
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ class SentryLog: NSObject {
static let alwaysLevel = SentryLevel.fatal
private static var logOutput = SentryLogOutput()
private static var logConfigureLock = NSLock()
private static var dateProvider: SentryCurrentDateProvider = SentryDefaultCurrentDateProvider()

@objc
static func configure(_ isDebug: Bool, diagnosticLevel: SentryLevel) {
@@ -26,7 +27,14 @@ class SentryLog: NSObject {
@objc
static func log(message: String, andLevel level: SentryLevel) {
guard willLog(atLevel: level) else { return }
logOutput.log("[Sentry] [\(level)] \(message)")

// We use the uptime because it's guaranteed to be linear even when the
// date time changes. Furthermore, initializing date objects and using the
// date format adds some overhead. The uptime shows the time difference
// between the logs. If we ever need the actual date and time, we can
// reconsider.
let uptime = nanosecondsToTimeInterval(self.dateProvider.systemTime())
logOutput.log("[Sentry] [\(level)] [uptime:\(uptime)] \(message)")
}

/**
@@ -54,6 +62,10 @@ class SentryLog: NSObject {
return logOutput
}

static func setDateProvider(_ dateProvider: SentryCurrentDateProvider) {
self.dateProvider = dateProvider
}

#endif
}

6 changes: 6 additions & 0 deletions Tests/SentryTests/Helper/SentryLog.swift
Original file line number Diff line number Diff line change
@@ -21,4 +21,10 @@ extension Sentry.SentryLog {
SentryLogOutput()
#endif
}

static func setCurrentDateProvider(_ dateProvider: SentryCurrentDateProvider) {
#if SENTRY_TEST || SENTRY_TEST_CI
SentryLog.setDateProvider(dateProvider)
#endif
}
}
24 changes: 15 additions & 9 deletions Tests/SentryTests/Helper/SentryLogTests.swift
Original file line number Diff line number Diff line change
@@ -6,18 +6,24 @@ class SentryLogTests: XCTestCase {
private var oldDebug: Bool!
private var oldLevel: SentryLevel!
private var oldOutput: SentryLogOutput!
private let systemUptime: TimeInterval = 0.1234

override func setUp() {
super.setUp()
oldDebug = SentryLog.isDebug
oldLevel = SentryLog.diagnosticLevel
oldOutput = SentryLog.getLogOutput()

let currentDateProvider = TestCurrentDateProvider()
currentDateProvider.advance(by: systemUptime)
SentryLog.setCurrentDateProvider(currentDateProvider)
}

override func tearDown() {
super.tearDown()
SentryLog.configure(oldDebug, diagnosticLevel: oldLevel)
SentryLog.setLogOutput(oldOutput)
SentryLog.setCurrentDateProvider(SentryDefaultCurrentDateProvider())
}

func testDefault_PrintsFatalAndError() {
@@ -30,7 +36,7 @@ class SentryLogTests: XCTestCase {
SentryLog.log(message: "2", andLevel: SentryLevel.warning)
SentryLog.log(message: "3", andLevel: SentryLevel.none)

XCTAssertEqual(["[Sentry] [fatal] 0", "[Sentry] [error] 1"], logOutput.loggedMessages)
XCTAssertEqual(["[Sentry] [fatal] [uptime:\(systemUptime)] 0", "[Sentry] [error] [uptime:\(systemUptime)] 1"], logOutput.loggedMessages)
}

func testDefaultInitOfLogoutPut() {
@@ -53,7 +59,7 @@ class SentryLogTests: XCTestCase {

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

func testLevelNone_PrintsEverythingExceptNone() {
@@ -68,11 +74,11 @@ class SentryLogTests: XCTestCase {
SentryLog.log(message: "4", andLevel: SentryLevel.debug)
SentryLog.log(message: "5", andLevel: SentryLevel.none)

XCTAssertEqual(["[Sentry] [fatal] 0",
"[Sentry] [error] 1",
"[Sentry] [warning] 2",
"[Sentry] [info] 3",
"[Sentry] [debug] 4"], logOutput.loggedMessages)
XCTAssertEqual(["[Sentry] [fatal] [uptime:\(systemUptime)] 0",
"[Sentry] [error] [uptime:\(systemUptime)] 1",
"[Sentry] [warning] [uptime:\(systemUptime)] 2",
"[Sentry] [info] [uptime:\(systemUptime)] 3",
"[Sentry] [debug] [uptime:\(systemUptime)] 4"], logOutput.loggedMessages)
}

func testMacroLogsErrorMessage() {
@@ -82,7 +88,7 @@ class SentryLogTests: XCTestCase {

sentryLogErrorWithMacro("error")

XCTAssertEqual(["[Sentry] [error] [SentryLogTestHelper:21] error"], logOutput.loggedMessages)
XCTAssertEqual(["[Sentry] [error] [uptime:\(systemUptime)] [SentryLogTestHelper:21] error"], logOutput.loggedMessages)
}

func testMacroDoesNotEvaluateArgs_WhenNotMessageNotLogged() {
@@ -101,7 +107,7 @@ class SentryLogTests: XCTestCase {
SentryLog.configure(true, diagnosticLevel: SentryLevel.debug)
let line = #line + 1
SentryLog.debug("Debug Log")
XCTAssertEqual(["[Sentry] [debug] [SentryLogTests:\(line)] Debug Log"], logOutput.loggedMessages)
XCTAssertEqual(["[Sentry] [debug] [uptime:\(systemUptime)] [SentryLogTests:\(line)] Debug Log"], logOutput.loggedMessages)
}

}

Unchanged files with check annotations Beta

}
dataTask.resume()
wait(for: [expect], timeout: 5)

Check failure on line 174 in Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerIntegrationTests.swift

GitHub Actions / Unit Catalyst - Xcode 15.4 - OS latest Sentry

testGetRequest_SpanCreatedAndBaggageHeaderAdded, Asynchronous wait failed: Exceeded timeout of 5 seconds, with unfulfilled expectations: "Request completed".
let children = Dynamic(transaction).children as [Span]?
return
}
guard let child = children.first(where: { $0.operation == "http.client" && $0.data["url"] as? String == "https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" && $0.data["http.response.status_code"] as? String == "200" }) else {

Check warning on line 71 in Samples/iOS-Swift/iOS-Swift/ViewControllers/TraceTestViewController.swift

GitHub Actions / UI Tests for iOS-Swift iPhone 14 (16.4) Simulator

value 'child' was defined but never used; consider replacing with boolean test
UIAssert.fail("Did not find child span for HTTP for retrieving the sentry brand logo.")
return
}
options.enableNetworkBreadcrumbs = enableNetworkBreadcrumbs
options.enableSwizzling = enableSwizzling
options.enableCrashHandler = enableCrashHandling
options.enableTracing = enableTracing

Check warning on line 55 in Samples/iOS-Swift/iOS-Swift/SentrySDKWrapper.swift

GitHub Actions / UI Tests for iOS-Swift iPhone 14 (16.4) Simulator

'enableTracing' is deprecated: Use tracesSampleRate or tracesSampler instead
options.enablePersistingTracesWhenCrashing = true
options.attachScreenshot = enableAttachScreenshot
options.attachViewHierarchy = enableAttachViewHierarchy
func assertFormHookFile(type: HookMarkerFile, exists: Bool) throws {
let path = try path(for: type)
if exists {
XCTAssert(fm.fileExists(atPath: path), "Expected file to exist at \(path)")

Check failure on line 544 in Samples/iOS-Swift/iOS-Swift-UITests/UserFeedbackUITests.swift

GitHub Actions / UI Tests for iOS-Swift iPhone 14 (16.4) Simulator

testSubmitWithNoFieldsFilledDefault, XCTAssertTrue failed - Expected file to exist at /Users/runner/Library/Developer/CoreSimulator/Devices/61EF8084-3E29-4EB2-84B3-35E7BA3D1946/data/Containers/Data/Application/F5C74FC6-184C-4EC5-B8AD-A74A280F9FB2/Library/Application Support/io.sentry/feedback/onSubmitError
} else {
XCTAssertFalse(fm.fileExists(atPath: path), "Expected file to not exist at \(path)")
}