Skip to content

Commit 451cd00

Browse files
committed
fix lo level filtering
1 parent fe0bffd commit 451cd00

File tree

3 files changed

+20
-118
lines changed

3 files changed

+20
-118
lines changed

3rd-party-integrations/SentryCocoaLumberjack/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ SentrySDK.start { options in
2929
options.enableLogs = true
3030
}
3131

32-
DDLog.add(SentryCocoaLumberjackLogger())
32+
DDLog.addLogger(SentryCocoaLumberjackLogger(), withLogLevel: .info)
3333

3434
DDLogInfo("User logged in")
3535
DDLogError("Payment failed")
@@ -42,14 +42,13 @@ DDLogVerbose("Detailed trace information")
4242

4343
### Log Level Threshold
4444

45-
Set the minimum log level for messages to be sent to Sentry:
45+
Use CocoaLumberjack's built-in filtering API when adding the logger to set the minimum log level for messages to be sent to Sentry:
4646

4747
```swift
48-
DDLog.add(SentryCocoaLumberjackLogger(logLevel: .verbose))
48+
// Only send error logs to Sentry
49+
DDLog.addLogger(SentryCocoaLumberjackLogger(), withLogLevel: .error)
4950
```
5051

51-
Messages below the configured threshold will be filtered out and not sent to Sentry.
52-
5352
## Log Level Mapping
5453

5554
CocoaLumberjack log levels are automatically mapped to Sentry log levels:

3rd-party-integrations/SentryCocoaLumberjack/Sources/SentryCocoaLumberjackLogger.swift

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import Sentry
88
/// application logs from CocoaLumberjack and send them to Sentry for analysis and monitoring.
99
///
1010
/// ## Level Filtering
11-
/// The logger supports filtering by log level. Only logs at or above the configured `logLevel` will be
12-
/// sent to Sentry. Defaults to `.info`.
11+
/// Use CocoaLumberjack's built-in filtering API when adding the logger:
12+
/// ```swift
13+
/// DDLog.addLogger(SentryCocoaLumberjackLogger(), withLogLevel: .info)
14+
/// ```
15+
/// This ensures only logs at or above the specified level are sent to Sentry.
1316
///
1417
/// ## Level Mapping
1518
/// CocoaLumberjack log levels are mapped to Sentry log levels:
@@ -32,7 +35,7 @@ import Sentry
3235
///
3336
/// // Add SentryCocoaLumberjackLogger to CocoaLumberjack
3437
/// // Only logs at .info level and above will be sent to Sentry
35-
/// DDLog.add(SentryCocoaLumberjackLogger(logLevel: .info))
38+
/// DDLog.addLogger(SentryCocoaLumberjackLogger(), withLogLevel: .info)
3639
///
3740
/// // Use CocoaLumberjack as usual
3841
/// DDLogInfo("User logged in")
@@ -43,39 +46,18 @@ import Sentry
4346
/// - Warning: This logger requires Sentry SDK to be initialized before use.
4447
public class SentryCocoaLumberjackLogger: DDAbstractLogger {
4548

46-
private let sentryLogger: SentryLogger
47-
48-
/// The minimum log level for messages to be sent to Sentry.
49-
///
50-
/// Messages below this level will be filtered out and not sent to Sentry.
51-
/// Defaults to `.info`.
52-
public var logLevel: DDLogLevel
53-
5449
/// Creates a new SentryCocoaLumberjackLogger instance.
5550
///
56-
/// - Parameter logLevel: The minimum log level for messages to be sent to Sentry.
57-
/// Defaults to `.info`.
5851
/// - Note: Make sure to initialize the Sentry SDK before creating this logger.
59-
public init(logLevel: DDLogLevel = .info) {
60-
self.sentryLogger = SentrySDK.logger
61-
self.logLevel = logLevel
62-
super.init()
63-
}
64-
65-
init(logLevel: DDLogLevel = .info, sentryLogger: SentryLogger) {
66-
self.sentryLogger = sentryLogger
67-
self.logLevel = logLevel
52+
/// Use `DDLog.addLogger(_:withLogLevel:)` to configure log level filtering.
53+
public init() {
6854
super.init()
6955
}
7056

7157
/// Logs a message from CocoaLumberjack to Sentry.
7258
///
7359
/// - Parameter logMessage: The log message from CocoaLumberjack containing the message, level, and metadata.
7460
public override func log(message logMessage: DDLogMessage) {
75-
guard logMessage.level.rawValue <= logLevel.rawValue else {
76-
return
77-
}
78-
7961
var attributes: [String: Any] = [:]
8062
attributes["sentry.origin"] = "auto.logging.cocoalumberjack"
8163

@@ -100,17 +82,17 @@ public class SentryCocoaLumberjackLogger: DDAbstractLogger {
10082

10183
private func forwardToSentry(message: String, flag: DDLogFlag, attributes: [String: Any]) {
10284
if flag.contains(.error) {
103-
sentryLogger.error(message, attributes: attributes)
85+
SentrySDK.logger.error(message, attributes: attributes)
10486
} else if flag.contains(.warning) {
105-
sentryLogger.warn(message, attributes: attributes)
87+
SentrySDK.logger.warn(message, attributes: attributes)
10688
} else if flag.contains(.info) {
107-
sentryLogger.info(message, attributes: attributes)
89+
SentrySDK.logger.info(message, attributes: attributes)
10890
} else if flag.contains(.debug) {
109-
sentryLogger.debug(message, attributes: attributes)
91+
SentrySDK.logger.debug(message, attributes: attributes)
11092
} else if flag.contains(.verbose) {
111-
sentryLogger.trace(message, attributes: attributes)
93+
SentrySDK.logger.trace(message, attributes: attributes)
11294
} else {
113-
sentryLogger.info(message, attributes: attributes)
95+
SentrySDK.logger.info(message, attributes: attributes)
11496
}
11597
}
11698

3rd-party-integrations/SentryCocoaLumberjack/Tests/SentryCocoaLumberjackLoggerTests.swift

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ final class SentryCocoaLumberjackLoggerTests: XCTestCase {
2828
capturedLogs = []
2929
}
3030

31-
private func getSut(logLevel: DDLogLevel = .all) -> SentryCocoaLumberjackLogger {
32-
return SentryCocoaLumberjackLogger(logLevel: logLevel)
31+
private func getSut() -> SentryCocoaLumberjackLogger {
32+
return SentryCocoaLumberjackLogger()
3333
}
3434

3535
// MARK: - Basic Logging Tests
@@ -291,85 +291,6 @@ final class SentryCocoaLumberjackLoggerTests: XCTestCase {
291291
XCTAssertEqual(timestampValue, 1_234_567_890.123, accuracy: 0.001)
292292
}
293293

294-
// MARK: - Log Level Filtering Tests
295-
296-
func testLogLevel_FiltersDebugWhenSetToInfo() {
297-
let sut = getSut(logLevel: .info)
298-
299-
let debugMessage = DDLogMessage(
300-
format: "Debug message",
301-
formatted: "Debug message",
302-
level: .debug,
303-
flag: .debug,
304-
context: 0,
305-
file: "TestFile.swift",
306-
function: "testFunction",
307-
line: 10,
308-
tag: nil,
309-
options: [],
310-
timestamp: Date()
311-
)
312-
313-
sut.log(message: debugMessage)
314-
315-
XCTAssertEqual(capturedLogs.count, 0, "Debug message should be filtered out when log level is .info")
316-
}
317-
318-
func testLogLevel_AllowsInfoWhenSetToInfo() {
319-
let sut = getSut(logLevel: .info)
320-
321-
let infoMessage = DDLogMessage(
322-
format: "Info message",
323-
formatted: "Info message",
324-
level: .info,
325-
flag: .info,
326-
context: 0,
327-
file: "TestFile.swift",
328-
function: "testFunction",
329-
line: 10,
330-
tag: nil,
331-
options: [],
332-
timestamp: Date()
333-
)
334-
335-
sut.log(message: infoMessage)
336-
337-
XCTAssertEqual(capturedLogs.count, 1, "Info message should be logged when log level is .info")
338-
}
339-
340-
func testLogLevel_AllowsErrorWhenSetToInfo() {
341-
let sut = getSut(logLevel: .info)
342-
343-
let errorMessage = DDLogMessage(
344-
format: "Error message",
345-
formatted: "Error message",
346-
level: .error,
347-
flag: .error,
348-
context: 0,
349-
file: "TestFile.swift",
350-
function: "testFunction",
351-
line: 10,
352-
tag: nil,
353-
options: [],
354-
timestamp: Date()
355-
)
356-
357-
sut.log(message: errorMessage)
358-
359-
XCTAssertEqual(capturedLogs.count, 1, "Error message should be logged when log level is .info")
360-
}
361-
362-
func testLogLevelConfiguration() {
363-
let sut = getSut()
364-
XCTAssertEqual(sut.logLevel, .all)
365-
366-
sut.logLevel = .info
367-
XCTAssertEqual(sut.logLevel, .info)
368-
369-
sut.logLevel = .error
370-
XCTAssertEqual(sut.logLevel, .error)
371-
}
372-
373294
// MARK: - Helper Methods
374295

375296
private func assertLogCaptured(

0 commit comments

Comments
 (0)