Skip to content

Commit 93d41f5

Browse files
committed
Merge branch 'feat/add-swift-log-integration' into feat/add-cocoalumberjack-integration
2 parents b112ac5 + aa78095 commit 93d41f5

File tree

1 file changed

+19
-96
lines changed
  • 3rd-party-integrations/SentrySwiftLog

1 file changed

+19
-96
lines changed

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

Lines changed: 19 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Sentry Swift-Log Integration
1+
# Sentry SwiftLog Integration
22

3-
A `swift-log` handler that forwards log entries to Sentry's structured logging system, automatically capturing application logs with full context including metadata, source location, and log levels.
3+
A [SwiftLog](https://github.com/apple/swift-log) handler that forwards log entries to Sentry's structured logging system, automatically capturing application logs with full context including metadata, source location, and log levels.
44

55
> [!NOTE]
66
> 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/SentrySwiftLog/`. This allows users to import only what they need via SPM while keeping all integration code in the main repository.
@@ -9,104 +9,66 @@ A `swift-log` handler that forwards log entries to Sentry's structured logging s
99

1010
### Swift Package Manager
1111

12-
Add the following dependencies to your `Package.swift`:
12+
Add the following dependencies to your `Package.swift` or Xcode package dependencies:
1313

1414
```swift
1515
dependencies: [
16-
.package(url: "https://github.com/getsentry/sentry-cocoa", from: "9.0.0"),
17-
.package(url: "https://github.com/getsentry/sentry-cocoa-swiftlog", from: "1.0.0"),
18-
.package(url: "https://github.com/apple/swift-log", from: "1.5.0")
16+
.package(url: "https://github.com/getsentry/sentry-cocoa-swiftlog", from: "1.0.0")
1917
]
2018
```
2119

22-
Then add the required products to your target dependencies:
23-
24-
```swift
25-
.target(
26-
name: "YourTarget",
27-
dependencies: [
28-
.product(name: "Sentry", package: "sentry-cocoa"), // Required for SentrySDK initialization
29-
.product(name: "SentrySwiftLog", package: "sentry-cocoa-swiftlog"),
30-
.product(name: "Logging", package: "swift-log") // Required for Logger and LoggingSystem
31-
]
32-
)
33-
```
34-
3520
## Quick Start
3621

37-
### 1. Initialize Sentry SDK
38-
39-
First, initialize the Sentry SDK with logs enabled:
40-
4122
```swift
4223
import Sentry
24+
import Logging
4325

4426
SentrySDK.start { options in
4527
options.dsn = "YOUR_DSN"
46-
options.enableLogs = true // Required for logging integration
28+
options.enableLogs = true
4729
}
48-
```
4930

50-
### 2. Register SentryLogHandler
51-
52-
Bootstrap the logging system with `SentryLogHandler`:
53-
54-
```swift
55-
import Logging
56-
57-
LoggingSystem.bootstrap { _ in
58-
return SentryLogHandler(logLevel: .info)
59-
}
60-
```
61-
62-
### 3. Use the Logger
31+
var handler = SentryLogHandler(logLevel: .info)
32+
handler.metadata["app_version"] = "1.0.0"
33+
handler.metadata["environment"] = "production"
6334

64-
Create and use loggers throughout your application:
35+
LoggingSystem.bootstrap { _ in handler }
6536

66-
```swift
6737
let logger = Logger(label: "com.example.app")
6838

6939
logger.info("User logged in", metadata: ["userId": "12345"])
7040
logger.error("Payment failed", metadata: ["errorCode": 500])
7141
logger.warning("API rate limit approaching", metadata: ["remaining": 10])
42+
43+
logger.info("User action", metadata: [
44+
"userId": "12345",
45+
"action": "purchase"
46+
])
7247
```
7348

7449
## Configuration
7550

7651
### Log Level Threshold
7752

78-
Set the minimum log level for messages to be sent to Sentry:
53+
Set the minimum log level for messages to be sent to Sentry. Messages below the configured threshold will be filtered out and not sent to Sentry.
7954

8055
```swift
81-
// Only send info level and above
82-
let handler = SentryLogHandler(logLevel: .info)
83-
84-
// Send all logs including trace and debug
8556
let handler = SentryLogHandler(logLevel: .trace)
8657
```
8758

88-
Messages below the configured threshold will be filtered out and not sent to Sentry.
89-
9059
### Handler Metadata
9160

92-
Add metadata that will be included with all log entries:
61+
Add metadata that will be included with all log entries. Handler metadata is merged with call-site metadata, with call-site metadata taking precedence.
9362

9463
```swift
9564
var handler = SentryLogHandler(logLevel: .info)
9665
handler.metadata["app_version"] = "1.0.0"
9766
handler.metadata["environment"] = "production"
98-
99-
LoggingSystem.bootstrap { _ in handler }
10067
```
10168

102-
Handler metadata is merged with call-site metadata, with call-site metadata taking precedence.
103-
104-
### Metadata Subscript Access
105-
10669
You can also access metadata using subscript syntax:
10770

10871
```swift
109-
var handler = SentryLogHandler(logLevel: .info)
11072
handler[metadataKey: "app_version"] = "1.0.0"
11173
let version = handler[metadataKey: "app_version"]
11274
```
@@ -127,47 +89,7 @@ let version = handler[metadataKey: "app_version"]
12789

12890
## Metadata Handling
12991

130-
The handler supports all `swift-log` metadata types:
131-
132-
### String Metadata
133-
134-
```swift
135-
logger.info("User action", metadata: [
136-
"userId": "12345",
137-
"action": "purchase"
138-
])
139-
```
140-
141-
### Dictionary Metadata
142-
143-
```swift
144-
logger.info("User profile", metadata: [
145-
"user": [
146-
"id": "12345",
147-
"name": "John Doe"
148-
]
149-
])
150-
```
151-
152-
### Array Metadata
153-
154-
```swift
155-
logger.info("Tags", metadata: [
156-
"tags": ["production", "api", "v2"]
157-
])
158-
```
159-
160-
### String Convertible Metadata
161-
162-
```swift
163-
logger.info("Metrics", metadata: [
164-
"count": 42,
165-
"enabled": true,
166-
"score": 3.14159
167-
])
168-
```
169-
170-
All metadata is automatically prefixed with `swift-log.` in Sentry attributes (e.g., `swift-log.userId`, `swift-log.user.id`).
92+
The handler supports all `swift-log` metadata types including strings, dictionaries, arrays, and string convertible types (numbers, booleans, etc.). All metadata is automatically prefixed with `swift-log.` in Sentry attributes (e.g., `swift-log.userId`, `swift-log.user.id`). See the Quick Start section above for examples of each metadata type.
17193

17294
## Automatic Attributes
17395

@@ -184,6 +106,7 @@ The handler automatically includes the following attributes with every log entry
184106

185107
- [Sentry Cocoa SDK Documentation](https://docs.sentry.io/platforms/apple/)
186108
- [Sentry Logs Documentation](https://docs.sentry.io/platforms/apple/logs/)
109+
- [SwiftLog Repo](https://github.com/apple/swift-log)
187110

188111
## License
189112

0 commit comments

Comments
 (0)