|
1 | 1 | # LoggerMiddleware |
2 | 2 |
|
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 | +``` |
0 commit comments