From f08b09d781e79f7cc2a30661598b82a0d6a9239a Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Fri, 21 Oct 2022 13:18:52 +0200
Subject: [PATCH] Setup logging when app starts

---
 .../xx-messenger/Sources/AppFeature/App.swift | 11 +++++---
 .../Sources/AppFeature/AppComponent.swift     |  6 +++++
 .../AppFeatureTests/AppComponentTests.swift   | 25 +++++++++++++++++++
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/Examples/xx-messenger/Sources/AppFeature/App.swift b/Examples/xx-messenger/Sources/AppFeature/App.swift
index cbaddc0f..c395f2ad 100644
--- a/Examples/xx-messenger/Sources/AppFeature/App.swift
+++ b/Examples/xx-messenger/Sources/AppFeature/App.swift
@@ -7,14 +7,17 @@ import SwiftUI
 struct App: SwiftUI.App {
   init() {
     LoggingSystem.bootstrap(PersistentLogHandler.init)
+    ViewStore(store.stateless).send(.setupLogging)
   }
 
+  let store = Store(
+    initialState: AppComponent.State(),
+    reducer: AppComponent()
+  )
+
   var body: some Scene {
     WindowGroup {
-      AppView(store: Store(
-        initialState: AppComponent.State(),
-        reducer: AppComponent()
-      ))
+      AppView(store: store)
     }
   }
 }
diff --git a/Examples/xx-messenger/Sources/AppFeature/AppComponent.swift b/Examples/xx-messenger/Sources/AppFeature/AppComponent.swift
index a8af5004..d092a69e 100644
--- a/Examples/xx-messenger/Sources/AppFeature/AppComponent.swift
+++ b/Examples/xx-messenger/Sources/AppFeature/AppComponent.swift
@@ -23,6 +23,7 @@ struct AppComponent: ReducerProtocol {
   }
 
   enum Action: Equatable, BindableAction {
+    case setupLogging
     case start
     case stop
     case binding(BindingAction<State>)
@@ -55,6 +56,11 @@ struct AppComponent: ReducerProtocol {
       let log = self.log
 
       switch action {
+      case .setupLogging:
+        _ = try! messenger.setLogLevel(.debug)
+        messenger.startLogging()
+        return .none
+
       case .start, .welcome(.finished), .restore(.finished), .home(.deleteAccount(.success)):
         state.screen = .loading
         return Effect.run { subscriber in
diff --git a/Examples/xx-messenger/Tests/AppFeatureTests/AppComponentTests.swift b/Examples/xx-messenger/Tests/AppFeatureTests/AppComponentTests.swift
index dff7d22a..7533666e 100644
--- a/Examples/xx-messenger/Tests/AppFeatureTests/AppComponentTests.swift
+++ b/Examples/xx-messenger/Tests/AppFeatureTests/AppComponentTests.swift
@@ -9,6 +9,29 @@ import XXClient
 @testable import AppFeature
 
 final class AppComponentTests: XCTestCase {
+  func testSetupLogging() {
+    var actions: [Action] = []
+
+    let store = TestStore(
+      initialState: AppComponent.State(),
+      reducer: AppComponent()
+    )
+    store.dependencies.app.messenger.setLogLevel.run = { level in
+      actions.append(.didSetLogLevel(level))
+      return true
+    }
+    store.dependencies.app.messenger.startLogging.run = {
+      actions.append(.didStartLogging)
+    }
+
+    store.send(.setupLogging)
+
+    XCTAssertNoDifference(actions, [
+      .didSetLogLevel(.debug),
+      .didStartLogging,
+    ])
+  }
+
   func testStartWithoutMessengerCreated() {
     var actions: [Action]!
 
@@ -514,4 +537,6 @@ private enum Action: Equatable {
   case didCancelBackupCallback
   case didLog(Logger.Message)
   case didStoreBackup(Data)
+  case didSetLogLevel(LogLevel)
+  case didStartLogging
 }
-- 
GitLab