Skip to content

Commit 309ce11

Browse files
committed
Update docs
1 parent fcb3143 commit 309ce11

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

Sources/Pulse/NetworkLogger/NetworkLogger+URLSession.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,44 @@ extension NetworkLogger {
1010
/// If enabled, registers ``RemoteLoggerURLProtocol``
1111
public var isMockingEnabled = true
1212

13-
/// A custom logger to be used instead of ``NetworkLogger/shared``.
14-
public var logger: NetworkLogger?
15-
1613
/// Creates default options.
1714
public init() {}
1815
}
1916

2017
public final class URLSession {
2118
/// The underlying `URLSession`.
2219
public let session: Foundation.URLSession
23-
var logger: NetworkLogger { options.logger ?? .shared}
20+
var logger: NetworkLogger { _logger ?? .shared }
21+
private let _logger: NetworkLogger?
2422
private let options: URLSessionOptions
2523

24+
/// - parameter logger: A custom logger to use instead of ``NetworkLogger/shared``.
2625
public convenience init(
2726
configuration: URLSessionConfiguration,
27+
logger: NetworkLogger? = nil,
2828
options: URLSessionOptions = .init()
2929
) {
3030
self.init(configuration: configuration, delegate: nil, delegateQueue: nil, options: options)
3131
}
3232

33+
/// - parameter logger: A custom logger to use instead of ``NetworkLogger/shared``.
3334
public init(
3435
configuration: URLSessionConfiguration,
3536
delegate: (any URLSessionDelegate)?,
3637
delegateQueue: OperationQueue? = nil,
38+
logger: NetworkLogger? = nil,
3739
options: URLSessionOptions = .init()
3840
) {
3941
if options.isMockingEnabled {
4042
configuration.protocolClasses = [RemoteLoggerURLProtocol.self] + (configuration.protocolClasses ?? [])
4143
}
4244
self.session = Foundation.URLSession(
4345
configuration: configuration,
44-
delegate: URLSessionProxyDelegate(logger: options.logger, delegate: delegate),
46+
delegate: URLSessionProxyDelegate(logger: logger, delegate: delegate),
4547
delegateQueue: delegateQueue
4648
)
4749
self.options = options
50+
self._logger = logger
4851
}
4952
}
5053
}

Sources/Pulse/Pulse.docc/Articles/GettingStarted.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ NetworkLogger.enableProxy()
3030
#endif
3131
```
3232

33-
> Note: **PulseProxy** uses method swizzling and private APIs and it is not recommended that you include it in the production builds of your app.
33+
> note: **PulseProxy** uses method swizzling and private APIs and it is not recommended that you include it in the production builds of your app.
3434
3535
- **Option 2 (Recommended)**. Use ``NetworkLogger/URLSession``, a thin wrapper on top of `URLSession`.
3636

@@ -45,7 +45,7 @@ session = URLSession(configuration: .default)
4545
#endif
4646
```
4747

48-
> Tip: See <doc:NetworkLogging-Article> for more information about how to configure network logging if your app does not use `URLSession` directly, how to further customize it, how to capture and display decoding errors, and more. Pulse is modular and will accommodate almost any system.
48+
> tip: See <doc:NetworkLogging-Article> for more information about how to configure network logging if your app does not use `URLSession` directly, how to further customize it, how to capture and display decoding errors, and more. Pulse is modular and will accommodate almost any system.
4949
5050
### 2.2. Collect Regular Messages
5151

Sources/Pulse/Pulse.docc/Articles/NetworkLogging-Article.md

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Pulse works on the `URLSession` level, and it needs access to its callbacks to l
1313

1414
The first step is to capture network traffic.
1515

16-
- **Option 1 (Quickest)**. If you are evaluating the framework, the quickest way to get started is with a proxy from the **PulseProxy** module.
16+
### Option 1 (Quickest)
17+
18+
If you are evaluating the framework, the quickest way to get started is with a proxy from the **PulseProxy** module.
1719

1820
```swift
1921
import PulseProxy
@@ -23,9 +25,11 @@ NetworkLogger.enableProxy()
2325
#endif
2426
```
2527

26-
> Important: **PulseProxy** uses method swizzling and private APIs, and it is not recommended that you include it in the production builds of your app. It is also not guaranteed to continue working with new versions of the system SDKs.
28+
> important: **PulseProxy** uses method swizzling and private APIs, and it is not recommended that you include it in the production builds of your app. It is also not guaranteed to continue working with new versions of the system SDKs.
29+
30+
### Option 2 (Recommended)
2731

28-
- **Option 2 (Recommended)**. Use ``NetworkLogger/URLSession``, a thin wrapper on top of `URLSession`.
32+
Use ``NetworkLogger/URLSession``, a thin wrapper on top of `URLSession`.
2933

3034
```swift
3135
import Pulse
@@ -40,16 +44,18 @@ session = URLSession(configuration: .default)
4044

4145
``NetworkLogger/URLSession`` is the best way to integrate Pulse because it supports all `URLSession` APIs, including the new Async/Await methods. It also makes it easy to remove it conditionally.
4246

43-
- **Option 3.** Use ``URLSessionProxyDelegate``.
47+
### Option 3
4448

45-
If you use a delegate-based `URLSession` that doesn't rely on any of its convenience APIs, such as [Alamofire](https://github.com/Alamofire/Alamofire), you can record its traffic using a proxy delegate.
49+
If you use a delegate-based `URLSession` that doesn't rely on any of its convenience APIs, such as [Alamofire](https://github.com/Alamofire/Alamofire), you can record its traffic using ``NetworkLogger/URLSessionProxyDelegate``.
4650

4751
```swift
48-
let delegate = URLSessionProxyDelegate(delegate: <#YourSessionDelegate#>)
52+
let delegate = NetworkLogger.URLSessionProxyDelegate(delegate: <#YourSessionDelegate#>)
4953
let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil)
5054
```
5155

52-
- **Option 4 (Manual).** Use ``NetworkLogger`` directly.
56+
> important: This method supports a limited subset of scenarios and doesn't work with `URLSession` Async/Await APIs.
57+
58+
### Option 4 (Manual)
5359

5460
If none of the convenience APIs described earlier work for you, you can use the underlying ``NetworkLogger`` directly. For example, here is how you can use it with Alamofire's `EventMonitor`:
5561

@@ -79,41 +85,26 @@ struct NetworkLoggerEventMonitor: EventMonitor {
7985
}
8086
```
8187

82-
> tip: Make sure to capture [`URLSessionTaskMetrics`](https://developer.apple.com/documentation/foundation/urlsessiontaskmetrics) as Pulse makes a great use of them and many of the features won't work without them.
83-
84-
## Session Proxy (Experimental)
85-
86-
To capture _all_ network traffic from _all_ session, including `URLSession.shared`, you can try using ``Experimental/URLSessionProxy``.
87-
88-
```swift
89-
Experimental.URLSessionProxy.shared.isEnabled = true
90-
```
91-
92-
> warning: As clearly communicate by its namespace, it's an experimental feature and it might negatively affect your networking. The way it works is by registering a custom [URLProtocol](https://developer.apple.com/documentation/foundation/urlprotocol) and using a secondary URLSession instance in it, but it can be a useful tool.
88+
## Configure Logging
9389

94-
> note: Alternatively, you can give the following swizzle-based [approach](https://gist.github.com/kean/3154a5bde8e0c5e9dc3322f21ba86757) a try that is less intrusive but requires more swizzling and can't be shipped with the production code.
90+
### Recod Decoding Errors
9591

96-
## Recoding Decoding Errors
97-
98-
The network requests usually can only be considered successful when the app was able to decode the response data. With Pulse, you can do just that and when you open the response body, it'll even highlight the part of the response that's causing the decoding error.
92+
The network requests can only be considered successful when the app decodes the response data. With Pulse, you can do just that, and when you open the response body, it'll even highlight the part of the response causing the decoding error.
9993

10094
```swift
10195
// Initial setup
102-
let logger = NetworkLogger(configuration: .init(isWaitingForDecoding: true))
103-
let delegate = URLSessionProxyDelegate(logger: logger, delegate: YourURLSessionDelegate()))
104-
// ... create session
96+
let logger = NetworkLogger {
97+
$0.isWaitingForDecoding = true
98+
}
99+
let session = NetworkLogger.URLSession(configuration: .default, logger: logger)
105100

106-
// Somewhere else in the app where decoding is done.
101+
// Add this to the code that performs decoding of the responses.
107102
logger.logTask(task, didFinishDecodingWithError: decodingError)
108103
```
109104

110-
## Exclude Information From Logs
111-
112-
There is usually some sensitive information in network requests, such as passwords, access tokens, and more. It's important to keep it safe.
113-
114-
> tip: It's recommended to use Pulse _only_ in the debug mode.
105+
### Exclude Information From Logs
115106

116-
``NetworkLogger`` captures data safely in a local database and it never leaves your device. Logs are never written to the system's logging system. But of course, logs are meant to be viewed and shared, which is why PulseUI provides sharing options. In case the logs do leave your device, it's best to redact any sensitive information.
107+
``NetworkLogger`` captures data safely in a local database, and it never leaves your device. Logs are never written to the system's logging system. But, of course, logs are meant to be viewed and shared, which is why PulseUI provides sharing options. In case the logs do leave your device, it's best to redact any sensitive information. 
117108

118109
``NetworkLogger/Configuration`` has a set of convenience APIs for managing what information is included or excluded from the logs.
119110

@@ -139,15 +130,21 @@ let logger = NetworkLogger {
139130
}
140131
```
141132

142-
> tip: "Include" and "exclude" patterns support basic wildcards (`*`), but you can also turns them into full-featured regex patterns using ``NetworkLogger/Configuration/isRegexEnabled``.
133+
You can then replace the default decoder with your custom instance:
134+
135+
```swift
136+
NetworkLogger.shared = logger
137+
```
138+
139+
> Tip: "Include" and "exclude" patterns support basic wildcards (`*`), but you can also turn them into full-featured regex patterns using ``NetworkLogger/Configuration/isRegexEnabled``
143140
144-
If the built-in configuration options don't cover all of your use-cases, you can set ``NetworkLogger/Configuration/willHandleEvent`` closure that provides you complete control for filtering out and updating the events.
141+
If the built-in configuration options don't cover all of your use cases, you can set the ``NetworkLogger/Configuration/willHandleEvent`` closure that provides you complete control for filtering out and updating the events.
145142

146-
> important: If you redact information manually from requests or responses, make sure to also update ``NetworkLogger/Metrics`` because individual transactions within metrics contain recorded request and response pairs.
143+
> important: If you redact information manually from requests or responses, also update ``NetworkLogger/Metrics`` because individual transactions within metrics contain recorded request and response pairs.
147144
148-
## Trace in Xcode Console
145+
### Trace in Xcode Console
149146

150-
While Pulse doesn't print anything in the Xcode Console by default, it's easy to enable such logging for network requests. ``LoggerStore`` re-translates all of the log events that it processes using ``LoggerStore/events`` publisher that you can leverage to log some of the recoded information in the Xcode Console.
147+
Pulse doesn't print anything in the Xcode Console by default, but it's easy to enable logging for network requests. ``LoggerStore`` re-translates all of the log events that it processes using ``LoggerStore/events`` publisher that you can leverage.
151148

152149
```swift
153150
func register(store: LoggerStore) {

Sources/Pulse/Pulse.docc/Pulse.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Logger and network inspector for Apple platforms.
77
### Essentials
88

99
- <doc:GettingStarted>
10-
- <doc:NetworkLogging-Article>
1110
- ``LoggerStore``
1211

1312
### Network Logging
1413

14+
- <doc:NetworkLogging-Article>
1515
- ``NetworkLogger``
1616
- ``URLSessionProtocol``
1717
- ``URLSessionProxyDelegate``

0 commit comments

Comments
 (0)