Skip to content

Commit

Permalink
Prep release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hkellaway committed Apr 22, 2021
1 parent 0625906 commit cfcae1e
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 10 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Change Log
All notable changes to this project will be documented in this file. [Semver](http://semver.org/) is used for versioning.
All notable changes to this project will be documented in this file. [Semver](http://semver.org/) is

- `0.1.x` Releases - [0.1.0](#010)
- `0.x.x` Releases - [0.1.0](#010) | [0.2.0](#020)

---

## [0.2.0](https://github.com/hkellaway/Nog/releases/tag/0.2.0)
Released on 2021-04-22.

#### Added
- UI for displaying logged requests
- cURL descriptions for requests
- Ability to filter logged requests
- Ability to turn off debug logging

## [0.1.0](https://github.com/hkellaway/Nog/releases/tag/0.1.0)
Released on 2021-03-28.

Expand Down
8 changes: 4 additions & 4 deletions Demo/NogDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ extension NetworkLogger {

struct ContentView: View {

let networkLogger: NetworkLogger = .custom
let networkLogger: NetworkLogger = .custom
let session: URLSession = .init(configuration: .default)
@State var isLogging = false
@State var isPresentingLog = false

var body: some View {
VStack {
Group {
Button("Make Request", action: makeRequest)
Button("Present Log", action: self.presentLog)
Button("Present Log", action: { isPresentingLog = true })
Button("\(isLogging ? "Stop" : "Start") Logging", action: toggleLogging)
.foregroundColor(isLogging ? .red : .green)
}
Expand All @@ -59,7 +59,7 @@ struct ContentView: View {
}

func makeRequest() {
guard let url = URL(string: "https://api.github.com/repos/hkellaway/Nog") else {
guard let url = URL(string: "https://api.github.com/zen") else {
return
}
self.session.dataTask(with: URLRequest(url: url)).resume()
Expand Down
2 changes: 1 addition & 1 deletion Nog.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Nog"
s.version = "0.1.0"
s.version = "0.2.0"
s.summary = "A delicious network request logger in Swift"
s.description = "A delicious network request logger in Swift."
s.homepage = "https://github.com/hkellaway/Nog"
Expand Down
140 changes: 137 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Swift](https://img.shields.io/badge/Swift-5.3-orange.svg)](https://swift.org/about/)
[![SPM](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://swift.org/package-manager/)
[![License](https://img.shields.io/badge/License-MIT-lightgray.svg)](https://raw.githubusercontent.com/hkellaway/Nog/main/LICENSE)
[![Build Status](https://github.com/hkellaway/Nog/actions/workflows/build.yml/badge.svg)](https://github.com/hkellaway/Nog/actions)
[![Build Status](https://github.com/hkellaway/Nog/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/hkellaway/Nog/actions)

## Usage

Expand Down Expand Up @@ -36,11 +36,145 @@ myNetworkLogger.isLogging

See the [Demo](/Demo) project for sample usage.

### Filtering Requests

By default Nog will print out only requests that start with `https` or `http`. However, you can introduce your own additional filter to specify when requests should or shouldn't be logged.

Simply initialize `NetworkLogger` with a function that takes in a `URLRequest` and returns a `Bool`:

``` swift
let myNetworkLogger = NetworkLogger(filter: {
$0.url?.absoluteString.contains("github") ?? false
})
```

### Displaying Requests

By default, Nog will display requests by printing them to console. If you'd prefer to provide custom UI, simply attach a view that conforms to `NetworkLogDisplayable` (or inherits from `NetworkLoggerViewController`):

``` swift
let myCustomView: NetworkLogDisplayable = MyNetworkLoggerView()
let myNetworkLogger = NetworkLogger()
myNetworkLogger.attachView(myCustomView)
```

#### Displaying Requests with SwiftUI

A SwiftUI wrapper for `NetworkLoggerViewController` is provided called `NetworkLoggerView`. Simply present:

``` swift
let networkLogger: NetworkLogger
@State var isPresentingLog = false

var body: some View {
Group {
...
Button("Present Log", action: { isPresentingLog = true })
}
.sheet(isPresented: $isPresentingLog) {
NetworkLoggerView(networkLogger: networkLogger)
}
}
```

#### Displaying Requests in cURL Format

When using `NetworkLoggerViewController`, the cURL representation of requests is right-at-hand.

Simply tap a request to view it's cURL representation. The **Debug** menu can be used to copy the cURL description for the last request viewed

### Advanced Usage

#### Debug Logging

By default, Nog will print messages to console to assist with debugging. Debug logs are appended with `[Nog]` to help isolate in console.

To turn off debug logging, either initialize with `verbose: false` or set at a later time:

``` swift
let quietNetworkLogger = NetworkLogger(requestFilters: [httpOnlyRequestFilter], verbose: false)
```

``` swift
let myNetworkLogger = NetworkLogger()
myNetworkLogger.verbose = false
```

#### Additional Filtering

To fully customize filtering, you can create your own `RequestFilter`s and provide an array:

``` swift
let gitHubOnlyRequestFilter: RequestFilter = {
$0.url?.absoluteString.contains("github") ?? false
}
```

``` swift
let myNetworkLogger = NetworkLogger(requestFilters: [httpOnlyRequestFilter, gitHubOnlyRequestFilter])

```

Note: If you still want to filter out only HTTP requests like Nog does by default, make sure to include `httpOnlyRequestFilter` in the list.

#### Mocking Requests

In order to mock requests being made, create `NetworkLogger` with a custom instance of `NetworkLoggerUrlProtocolAdapter`:

``` swift
class MyNogAdapter: NetworkLoggerUrlProtocolAdapter {

static var shared = MyNogAdapter()

func sendMockRequest() {
requestReceived(.init(url: URL(string: "https://github.com/helloworld")!))
}

}
```

``` swift
let myNetworkLogger = NetworkLogger(requestFilters: [httpOnlyRequestFilter], adapter: MyNogAdapter.shared)
```

#### Custom NetworkLogger

To fully customize how `NetworkLogger` handles logging requests, create an instance of `NetworkLogger` and override `logRequest(_:)`:

``` swift
class MyNetworkLogger: NetworkLogger {

@discardableResult
override func logRequest(_ urlRequest: URLRequest) -> Result<(), NetworkLoggerError> {
print("Hello World \(requestCount) times")
return .success(())
}

}
```

``` swift
let myNetworkLogger: NetworkLogger = MyNetworkLogger()
```

Note: This will bypass evaluating `requestFilters`.

## Installation

### Installation with Swift Package Manager
### Swift Package Manager

Point to the [latest release](https://github.com/hkellaway/Nog/releases) or to the `main` branch for the latest.

### CocoaPods

See [Adding Package Dependencies to Your App](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app). Search for `Nog` with *Owner* `hkellaway`. Point to the desired version or the `main` branch for the latest.

```ruby
pod 'Nog', :git => 'https://github.com/hkellaway/Nog.git', :tag => 'x.x.x'
```

```ruby
pod 'Nog', :git => 'https://github.com/hkellaway/Nog.git', :branch => 'main'
```

## Credits

Expand Down

0 comments on commit cfcae1e

Please sign in to comment.