Created
August 9, 2023 13:56
-
-
Save darrarski/1905866ebf9cf299da1283f9296a0d8a to your computer and use it in GitHub Desktop.
ComposableArchitecture + SwiftLog integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ComposableArchitecture | |
import Logging | |
extension _ReducerPrinter { | |
/// Logs info about received actions and state changes to swift-log's Logger with provided label. | |
/// | |
/// Example usage: | |
/// ``` | |
/// let store = Store(initialState: AppFeature.State()) { | |
/// AppFeature()._printChanges(.swiftLog(label: "tca")) | |
/// } | |
/// ``` | |
/// | |
/// - Parameter label: Logger's label | |
public static func swiftLog(label: String) -> Self { | |
swiftLog(Logger(label: label)) | |
} | |
/// Logs info about received actions and state changes to provided swift-log's Logger. | |
/// | |
/// Example usage: | |
/// ``` | |
/// let store = Store(initialState: AppFeature.State()) { | |
/// AppFeature()._printChanges(.swiftLog(Logger(label: "tca"))) | |
/// } | |
/// ``` | |
/// | |
/// - Parameter logger: Logger | |
public static func swiftLog(_ logger: Logger) -> Self { | |
Self { receivedAction, oldState, newState in | |
var message = "received action:\n" | |
CustomDump.customDump(receivedAction, to: &message, indent: 2) | |
message.write("\n") | |
message.write(diff(oldState, newState).map { "\($0)\n" } ?? " (No state changes)\n") | |
logger.info(.init(stringLiteral: message)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment