-
-
Notifications
You must be signed in to change notification settings - Fork 372
Log Integrations: Add SentryCocoaLumberjack integration
#7020
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
Open
denrase
wants to merge
11
commits into
feat/add-swift-log-integration
Choose a base branch
from
feat/add-cocoalumberjack-integration
base: feat/add-swift-log-integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
730e39c
Add `SentryCocoaLumberjack` integration
denrase b112ac5
Add readme
denrase 93d41f5
Merge branch 'feat/add-swift-log-integration' into feat/add-cocoalumb…
denrase dbbb1cc
Merge branch 'feat/add-swift-log-integration' into feat/add-cocoalumb…
denrase 46e2760
Merge branch 'feat/add-swift-log-integration' into feat/add-cocoalumb…
denrase fe0bffd
Add tests to ci
denrase 451cd00
fix lo level filtering
denrase fcf566a
fix filtering
denrase 77bd1d7
Merge branch 'feat/add-swift-log-integration' into feat/add-cocoalumb…
denrase 3d0b276
add to update script
denrase c7ff522
remove tests for lumberjack functionality
denrase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
3rd-party-integrations/SentryCocoaLumberjack/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # --- macOS --- | ||
|
|
||
| # General | ||
| .DS_Store | ||
| __MACOSX/ | ||
| .AppleDouble | ||
| .LSOverride | ||
| Icon[] | ||
|
|
||
| # Thumbnails | ||
| ._* | ||
|
|
||
| # Files that might appear in the root of a volume | ||
| .DocumentRevisions-V100 | ||
| .fseventsd | ||
| .Spotlight-V100 | ||
| .TemporaryItems | ||
| .Trashes | ||
| .VolumeIcon.icns | ||
| .com.apple.timemachine.donotpresent | ||
|
|
||
| # Directories potentially created on remote AFP share | ||
| .AppleDB | ||
| .AppleDesktop | ||
| Network Trash Folder | ||
| Temporary Items | ||
| .apdisk | ||
|
|
||
| # --- Swift --- | ||
|
|
||
| # Xcode | ||
| # | ||
| # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
|
||
| ## User settings | ||
| xcuserdata/ | ||
|
|
||
| ## Obj-C/Swift specific | ||
| *.hmap | ||
|
|
||
| ## App packaging | ||
| *.ipa | ||
| *.dSYM.zip | ||
| *.dSYM | ||
|
|
||
| ## Playgrounds | ||
| timeline.xctimeline | ||
| playground.xcworkspace | ||
|
|
||
| # Swift Package Manager | ||
| # | ||
| # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
| # Packages/ | ||
| # Package.pins | ||
| # Package.resolved | ||
| # *.xcodeproj | ||
| # | ||
| # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata | ||
| # hence it is not needed unless you have added a package configuration file to your project | ||
| # .swiftpm | ||
|
|
||
| .build/ | ||
|
|
||
| # CocoaPods | ||
| # | ||
| # We recommend against adding the Pods directory to your .gitignore. However | ||
| # you should judge for yourself, the pros and cons are mentioned at: | ||
| # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
| # | ||
| # Pods/ | ||
| # | ||
| # Add this line if you want to avoid checking in source code from the Xcode workspace | ||
| # *.xcworkspace | ||
|
|
||
| # Carthage | ||
| # | ||
| # Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
| # Carthage/Checkouts | ||
|
|
||
| Carthage/Build/ | ||
|
|
||
| # fastlane | ||
| # | ||
| # It is recommended to not store the screenshots in the git repo. | ||
| # Instead, use fastlane to re-generate the screenshots whenever they are needed. | ||
| # For more information about the recommended setup visit: | ||
| # https://docs.fastlane.tools/best-practices/source-control/#source-control | ||
|
|
||
| fastlane/report.xml | ||
| fastlane/Preview.html | ||
| fastlane/screenshots/**/*.png | ||
| fastlane/test_output | ||
|
|
||
| # --- Xcode --- | ||
|
|
||
| ## User settings | ||
| xcuserdata/ | ||
|
|
||
| # Archive | ||
| *.xcarchive |
34 changes: 34 additions & 0 deletions
34
3rd-party-integrations/SentryCocoaLumberjack/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // swift-tools-version:6.0 | ||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "SentryCocoaLumberjack", | ||
| platforms: [.iOS(.v15), .macOS(.v10_14), .tvOS(.v15), .watchOS(.v8), .visionOS(.v1)], | ||
| products: [ | ||
| .library( | ||
| name: "SentryCocoaLumberjack", | ||
| targets: ["SentryCocoaLumberjack"] | ||
| ) | ||
| ], | ||
| dependencies: [ | ||
| .package(url: "https://github.com/CocoaLumberjack/CocoaLumberjack", from: "3.8.0"), | ||
| .package(url: "https://github.com/getsentry/sentry-cocoa", from: "9.0.0") | ||
| ], | ||
| targets: [ | ||
| .target( | ||
| name: "SentryCocoaLumberjack", | ||
| dependencies: [ | ||
| .product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"), | ||
| .product(name: "Sentry", package: "sentry-cocoa") | ||
| ] | ||
| ), | ||
| .testTarget( | ||
| name: "SentryCocoaLumberjackTests", | ||
| dependencies: [ | ||
| "SentryCocoaLumberjack", | ||
| .product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"), | ||
| .product(name: "Sentry", package: "sentry-cocoa") | ||
| ] | ||
| ) | ||
| ] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Sentry CocoaLumberjack Integration | ||
|
|
||
| A [CocoaLumberjack](https://github.com/CocoaLumberjack/CocoaLumberjack) logger that forwards log entries to Sentry's structured logging system, automatically capturing application logs with full context including source location, thread information, and log levels. | ||
|
|
||
| > [!NOTE] | ||
| > This repo is a mirror of [github.com/getsentry/sentry-cocoa](https://github.com/getsentry/sentry-cocoa). The source code lives in `3rd-party-integrations/SentryCocoaLumberjack/`. This allows users to import only what they need via SPM while keeping all integration code in the main repository. | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Swift Package Manager | ||
|
|
||
| Add the following dependencies to your `Package.swift` or Xcode package dependencies: | ||
|
|
||
| ```swift | ||
| dependencies: [ | ||
| .package(url: "https://github.com/getsentry/sentry-cocoa-cocoalumberjack", from: "1.0.0") | ||
| ] | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```swift | ||
| import Sentry | ||
| import SentryCocoaLumberjack | ||
| import CocoaLumberjackSwift | ||
|
|
||
| SentrySDK.start { options in | ||
| options.dsn = "YOUR_DSN" | ||
| options.enableLogs = true | ||
| } | ||
|
|
||
| DDLog.add(SentryCocoaLumberjackLogger()) | ||
|
|
||
| DDLogInfo("User logged in") | ||
| DDLogError("Payment failed") | ||
| DDLogWarn("API rate limit approaching") | ||
| DDLogDebug("Processing request") | ||
| DDLogVerbose("Detailed trace information") | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Log Level Threshold | ||
|
|
||
| Set the minimum log level for messages to be sent to Sentry: | ||
|
|
||
| ```swift | ||
| DDLog.add(SentryCocoaLumberjackLogger(logLevel: .verbose)) | ||
| ``` | ||
|
|
||
| Messages below the configured threshold will be filtered out and not sent to Sentry. | ||
|
|
||
| ## Log Level Mapping | ||
|
|
||
| CocoaLumberjack log levels are automatically mapped to Sentry log levels: | ||
|
|
||
| | CocoaLumberjack Level | Sentry Log Level | | ||
| | --------------------- | ---------------- | | ||
| | `.error` | `.error` | | ||
| | `.warning` | `.warn` | | ||
| | `.info` | `.info` | | ||
| | `.debug` | `.debug` | | ||
| | `.verbose` | `.trace` | | ||
|
|
||
| ## Automatic Attributes | ||
|
|
||
| The logger automatically includes the following attributes with every log entry: | ||
|
|
||
| - `sentry.origin`: `"auto.logging.cocoalumberjack"` | ||
| - `cocoalumberjack.level`: The original CocoaLumberjack log level (error, warning, info, debug, verbose) | ||
| - `cocoalumberjack.file`: The source file name | ||
| - `cocoalumberjack.function`: The function name | ||
| - `cocoalumberjack.line`: The line number | ||
| - `cocoalumberjack.context`: The log context (integer) | ||
| - `cocoalumberjack.timestamp`: The log timestamp | ||
| - `cocoalumberjack.threadID`: The thread ID | ||
| - `cocoalumberjack.threadName`: The thread name (if available) | ||
| - `cocoalumberjack.queueLabel`: The dispatch queue label (if available) | ||
|
|
||
| ## Documentation | ||
|
|
||
| - [Sentry Cocoa SDK Documentation](https://docs.sentry.io/platforms/apple/) | ||
| - [Sentry Logs Documentation](https://docs.sentry.io/platforms/apple/logs/) | ||
| - [CocoaLumberjack Repo](https://github.com/CocoaLumberjack/CocoaLumberjack) | ||
|
|
||
| ## License | ||
|
|
||
| This integration follows the same license as the Sentry Cocoa SDK. See the [LICENSE](https://github.com/getsentry/sentry-cocoa/blob/main/LICENSE.md) file for details. |
132 changes: 132 additions & 0 deletions
132
3rd-party-integrations/SentryCocoaLumberjack/Sources/SentryCocoaLumberjackLogger.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import CocoaLumberjackSwift | ||
| import Sentry | ||
|
|
||
| /// A CocoaLumberjack logger that forwards log entries to Sentry's structured logging system. | ||
| /// | ||
| /// `SentryCocoaLumberjackLogger` implements CocoaLumberjack's `DDAbstractLogger` protocol, allowing you | ||
| /// to integrate Sentry's structured logging capabilities with CocoaLumberjack. This enables you to capture | ||
| /// application logs from CocoaLumberjack and send them to Sentry for analysis and monitoring. | ||
| /// | ||
| /// ## Level Filtering | ||
| /// The logger supports filtering by log level. Only logs at or above the configured `logLevel` will be | ||
| /// sent to Sentry. Defaults to `.info`. | ||
| /// | ||
| /// ## Level Mapping | ||
| /// CocoaLumberjack log levels are mapped to Sentry log levels: | ||
| /// - `.error` → `.error` | ||
| /// - `.warning` → `.warn` | ||
| /// - `.info` → `.info` | ||
| /// - `.debug` → `.debug` | ||
| /// - `.verbose` → `.trace` | ||
| /// | ||
| /// ## Usage | ||
| /// ```swift | ||
| /// import CocoaLumberjackSwift | ||
| /// import Sentry | ||
| /// import SentryCocoaLumberjack | ||
| /// | ||
| /// // Initialize Sentry SDK | ||
| /// SentrySDK.start { options in | ||
| /// options.dsn = "YOUR_DSN" | ||
| /// } | ||
| /// | ||
| /// // Add SentryCocoaLumberjackLogger to CocoaLumberjack | ||
| /// // Only logs at .info level and above will be sent to Sentry | ||
| /// DDLog.add(SentryCocoaLumberjackLogger(logLevel: .info)) | ||
| /// | ||
| /// // Use CocoaLumberjack as usual | ||
| /// DDLogInfo("User logged in") | ||
| /// DDLogError("Payment failed") | ||
| /// ``` | ||
| /// | ||
| /// - Note: Sentry Logs is currently in Beta. See the [Sentry Logs Documentation](https://docs.sentry.io/platforms/apple/logs/). | ||
| /// - Warning: This logger requires Sentry SDK to be initialized before use. | ||
| public class SentryCocoaLumberjackLogger: DDAbstractLogger { | ||
|
|
||
| private let sentryLogger: SentryLogger | ||
|
|
||
| /// The minimum log level for messages to be sent to Sentry. | ||
| /// | ||
| /// Messages below this level will be filtered out and not sent to Sentry. | ||
| /// Defaults to `.info`. | ||
| public var logLevel: DDLogLevel | ||
|
|
||
| /// Creates a new SentryCocoaLumberjackLogger instance. | ||
| /// | ||
| /// - Parameter logLevel: The minimum log level for messages to be sent to Sentry. | ||
| /// Defaults to `.info`. | ||
| /// - Note: Make sure to initialize the Sentry SDK before creating this logger. | ||
| public init(logLevel: DDLogLevel = .info) { | ||
| self.sentryLogger = SentrySDK.logger | ||
| self.logLevel = logLevel | ||
| super.init() | ||
| } | ||
|
|
||
| init(logLevel: DDLogLevel = .info, sentryLogger: SentryLogger) { | ||
| self.sentryLogger = sentryLogger | ||
| self.logLevel = logLevel | ||
| super.init() | ||
| } | ||
|
|
||
| /// Logs a message from CocoaLumberjack to Sentry. | ||
| /// | ||
| /// - Parameter logMessage: The log message from CocoaLumberjack containing the message, level, and metadata. | ||
| public override func log(message logMessage: DDLogMessage) { | ||
| guard logMessage.level.rawValue <= logLevel.rawValue else { | ||
| return | ||
| } | ||
denrase marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| var attributes: [String: Any] = [:] | ||
| attributes["sentry.origin"] = "auto.logging.cocoalumberjack" | ||
|
|
||
| attributes["cocoalumberjack.level"] = logFlagToString(logMessage.flag) | ||
| attributes["cocoalumberjack.file"] = logMessage.file | ||
| attributes["cocoalumberjack.function"] = logMessage.function ?? "" | ||
| attributes["cocoalumberjack.line"] = String(logMessage.line) | ||
| attributes["cocoalumberjack.context"] = String(logMessage.context) | ||
| attributes["cocoalumberjack.timestamp"] = logMessage.timestamp.timeIntervalSince1970 | ||
| attributes["cocoalumberjack.threadID"] = String(logMessage.threadID) | ||
|
|
||
| if let threadName = logMessage.threadName, !threadName.isEmpty { | ||
| attributes["cocoalumberjack.threadName"] = threadName | ||
| } | ||
|
|
||
| if !logMessage.queueLabel.isEmpty { | ||
| attributes["cocoalumberjack.queueLabel"] = logMessage.queueLabel | ||
| } | ||
|
|
||
| forwardToSentry(message: logMessage.message, flag: logMessage.flag, attributes: attributes) | ||
| } | ||
|
|
||
| private func forwardToSentry(message: String, flag: DDLogFlag, attributes: [String: Any]) { | ||
| if flag.contains(.error) { | ||
| sentryLogger.error(message, attributes: attributes) | ||
| } else if flag.contains(.warning) { | ||
| sentryLogger.warn(message, attributes: attributes) | ||
| } else if flag.contains(.info) { | ||
| sentryLogger.info(message, attributes: attributes) | ||
| } else if flag.contains(.debug) { | ||
| sentryLogger.debug(message, attributes: attributes) | ||
| } else if flag.contains(.verbose) { | ||
| sentryLogger.trace(message, attributes: attributes) | ||
| } else { | ||
| sentryLogger.info(message, attributes: attributes) | ||
| } | ||
| } | ||
|
|
||
| private func logFlagToString(_ flag: DDLogFlag) -> String { | ||
| if flag.contains(.error) { | ||
| return "error" | ||
| } else if flag.contains(.warning) { | ||
| return "warning" | ||
| } else if flag.contains(.info) { | ||
| return "info" | ||
| } else if flag.contains(.debug) { | ||
| return "debug" | ||
| } else if flag.contains(.verbose) { | ||
| return "verbose" | ||
| } else { | ||
| return "unknown" | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.