Skip to content

Commit 29bd4ec

Browse files
committed
README and public helpers
1 parent d5bb6ae commit 29bd4ec

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,90 @@
11
# LoggerMiddleware
22

3-
A description of this package.
3+
## Usage
4+
5+
### Simple usage, logging all actions and state changes for whole app
6+
```swift
7+
LoggerMiddleware() <> MyOtherMiddleware().lift(...)
8+
```
9+
10+
### Log a single middleware, only actions and state within that middleware field
11+
```swift
12+
MyOtherMiddleware().logger().lift(...)
13+
```
14+
15+
### Log a single middleware, but including actions and state changes for the whole app
16+
(same as adding LoggerMiddleware in the chain as seen in the first option)
17+
```swift
18+
MyOtherMiddleware().lift(...).logger()
19+
```
20+
21+
### Log a specific group of actions and state tree
22+
```swift
23+
IdentityMiddleware<InterestingAction, InterestingAction, InterestingState>()
24+
.logger()
25+
.lift(...) // lift InterestingAction and InterestingState to AppAction and AppState
26+
<> MyOtherMiddleware().lift(...)
27+
```
28+
29+
## Parameters
30+
31+
### actionTransform
32+
Gives the Input Action and Action Source to be formatted as a string.
33+
34+
Default:
35+
```swift
36+
actionTransform: @escaping (InputActionType, ActionSource) -> String = {
37+
"\n🕹 \($0)\n🎪 \($1.file.split(separator: "/").last ?? ""):\($1.line) \($1.function)"
38+
}
39+
```
40+
41+
### actionPrinter
42+
Gives the action and action source string, formatted from previous parameter, to be logged or saved into a file
43+
44+
Default:
45+
```swift
46+
actionPrinter: @escaping (String) -> Void = { os_log(.debug, log: .default, "%{PUBLIC}@", $0) }
47+
```
48+
49+
### stateDiffTransform
50+
Gives the previous state, and the state after the reducers have changed it, so a diff string can be created.
51+
`Difference` struct contains helpers to compare multiline strings, and `dumpToString` free function is a helper to stringify anything.
52+
Alternatively you could stringify using JSONEncoder or any other tool. Be careful with performance, or provide an alternative queue
53+
to avoid locking the main queue with heavy log task.
54+
Returning `nil` means that nothing has changed.
55+
The default logger will give a "git diff" output, containing + and - for changed lines, including 2 lines of context before and after the change.
56+
57+
Default:
58+
```swift
59+
stateDiffTransform: @escaping (StateType?, StateType) -> String? = {
60+
let stateBefore = dumpToString($0)
61+
let stateAfter = dumpToString($1)
62+
return Difference.diff(old: stateBefore, new: stateAfter, linesOfContext: 2, prefixLines: "🏛 ")
63+
}
64+
```
65+
66+
### stateDiffPrinter
67+
68+
Gives the state diff string, formatted from previous parameter, to be logged or saved into a file.
69+
Receiving `nil` means that the state hasn't changed with this action.
70+
71+
Default:
72+
```swift
73+
stateDiffPrinter: @escaping (String?) -> Void = { state in
74+
if let state = state {
75+
os_log(.debug, log: .default, "%{PUBLIC}@", state)
76+
} else {
77+
os_log(.debug, log: .default, "%{PUBLIC}@", "🏛 No state mutation")
78+
}
79+
}
80+
```
81+
82+
### queue
83+
84+
The queue to run the string transformation and printer. Use an alternative, low priority, serial queue to avoid locking the UI
85+
with logging operations.
86+
87+
Default:
88+
```swift
89+
queue: DispatchQueue = .main
90+
```

Sources/LoggerMiddleware/Diff.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public func dumpToString<T>(_ something: T, indent: Int = 2) -> String {
77
}
88

99
public struct Difference<A> {
10-
enum Which {
10+
public enum Which {
1111
case first
1212
case second
1313
case both
1414
}
1515

16-
let elements: [A]
17-
let which: Which
16+
public let elements: [A]
17+
public let which: Which
1818
}
1919

2020
extension Difference where A == String {

Sources/LoggerMiddleware/LoggerMiddleware.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Middleware where StateType: Equatable {
3434
}
3535

3636
public final class LoggerMiddleware<M: Middleware, InputActionType, OutputActionType, StateType: Equatable>
37-
where M.StateType == StateType, M.InputActionType ==InputActionType, M.OutputActionType == OutputActionType {
37+
where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputActionType == OutputActionType {
3838
private let middleware: M
3939
private let queue: DispatchQueue
4040
private var getState: GetState<StateType>?

0 commit comments

Comments
 (0)