Swiftpack.co - SwiftRex/LoggerMiddleware as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by SwiftRex.
SwiftRex/LoggerMiddleware v0.3.8
SwiftRex Logger Middleware
ā­ļø 2
šŸ•“ 2 days ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/SwiftRex/LoggerMiddleware.git", from: "v0.3.8")



Simple usage, logging all actions and state changes for whole app

LoggerMiddleware.default() <> MyOtherMiddleware().lift(...)

Log a single middleware, only actions and state within that middleware field


Log a single middleware, but including actions and state changes for the whole app

(same as adding LoggerMiddleware.default() in the chain as seen in the first option)


Log a specific group of actions and state tree

IdentityMiddleware<InterestingAction, InterestingAction, InterestingState>()
    .lift(...) // lift InterestingAction and InterestingState to AppAction and AppState
<> MyOtherMiddleware().lift(...)



Gives the Input Action and Action Source to be formatted as a string.


actionTransform: @escaping (InputActionType, ActionSource) -> String = {
    "\nšŸ•¹ \($0)\nšŸŽŖ \($1.file.split(separator: "/").last ?? ""):\($1.line) \($1.function)"


Gives the action and action source string, formatted from previous parameter, to be logged or saved into a file


actionPrinter: @escaping (String) -> Void = { os_log(.debug, log: .default, "%{PUBLIC}@", $0) }


Gives the previous state, and the state after the reducers have changed it, so a diff string can be created. Difference struct contains helpers to compare multiline strings, and dumpToString free function is a helper to stringify anything. Alternatively you could stringify using JSONEncoder or any other tool. Be careful with performance, or provide an alternative queue to avoid locking the main queue with heavy log task. Returning nil means that nothing has changed. The default logger will give a "git diff" output, containing + and - for changed lines, including 2 lines of context before and after the change.


stateDiffTransform: @escaping (StateType?, StateType) -> String? = {
    let stateBefore = dumpToString($0)
    let stateAfter =  dumpToString($1)
    return Difference.diff(old: stateBefore, new: stateAfter, linesOfContext: 2, prefixLines: "šŸ› ")


Gives the state diff string, formatted from previous parameter, to be logged or saved into a file. Receiving nil means that the state hasn't changed with this action.


stateDiffPrinter: @escaping (String?) -> Void = { state in
    if let state = state {
        os_log(.debug, log: .default, "%{PUBLIC}@", state)
    } else {
        os_log(.debug, log: .default, "%{PUBLIC}@", "šŸ› No state mutation")


The queue to run the string transformation and printer. Use an alternative, low priority, serial queue to avoid locking the UI with logging operations.


queue: DispatchQueue = .main


Stars: 2
Last commit: 2 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.


Release Notes

Update SwiftRex and use Middleware IO
2 days ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics