Skip to content

Instantly share code, notes, and snippets.

@darrarski
Created August 9, 2023 13:56
Show Gist options
  • Save darrarski/1905866ebf9cf299da1283f9296a0d8a to your computer and use it in GitHub Desktop.
Save darrarski/1905866ebf9cf299da1283f9296a0d8a to your computer and use it in GitHub Desktop.
ComposableArchitecture + SwiftLog integration
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