Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IDLE-000] 로깅전송 객체 리팩토링 #97

Merged
merged 11 commits into from
Oct 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public enum ModuleDependency {
public static let PresentationCore: TargetDependency = .project(target: "PresentationCore", path: .relativeToRoot("Projects/Presentation/PresentationCore"))
}

public enum Module {
public static let Logger: TargetDependency = .project(target: "Logger", path: .relativeToRoot("Projects/Module/Logger"))
}

public static let Core: TargetDependency = .project(target: "Core", path: .relativeToRoot("Projects/Core"))

public static let Testing: TargetDependency = .project(target: "Testing", path: .relativeToRoot("Projects/Testing"))
Expand Down
32 changes: 7 additions & 25 deletions project/Projects/App/Sources/DIAssembly/LoggerAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,26 @@
//

import Foundation

import RootFeature
import AuthFeature
import PresentationCore
import CenterMainPageFeature
import Logger


import Swinject

public struct LoggerAssembly: Assembly {
public func assemble(container: Container) {

// MARK: Message pusher
container.register(LoggerMessagePublisher.self) { _ in
#if DEBUG || QA
return DebugLogger()
container.register(Logger.self) { _ in
#if DEBUG
return MockLogger()
#endif

return AmplitudeLogger()
}
.inObjectScope(.container)

// MARK: Overall logger
container.register(OverallLogger.self) { _ in
DefaultOverallLogger()
}
.inObjectScope(.container)

container.register(CenterRegisterLogger.self) { resolver in
let overallLogger = resolver.resolve(OverallLogger.self)!
return overallLogger
}

container.register(WorkerRegisterLogger.self) { resolver in
let overallLogger = resolver.resolve(OverallLogger.self)!
return overallLogger
}

container.register(PostRegisterLogger.self) { resolver in
let overallLogger = resolver.resolve(OverallLogger.self)!
return overallLogger
}
}
}
39 changes: 39 additions & 0 deletions project/Projects/Module/Logger/Project.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Project.swift
// ProjectDescriptionHelpers
//
// Created by choijunyeong on 2024/10/30.
//

import ProjectDescription
import ConfigurationPlugin
import DependencyPlugin
import Foundation

let project = Project(
name: "Logger",
settings: .settings(
configurations: IdleConfiguration.emptyConfigurations
),
targets: [
.target(
name: "Logger",
destinations: DeploymentSettings.platforms,
product: .framework,
bundleId: "$(PRODUCT_BUNDLE_IDENTIFIER)",
deploymentTargets: DeploymentSettings.deployment_iOS_version,
sources: [
"Sources/**",
SecretSource.amplitudeConfig,
],
resources: ["Resources/**",],
dependencies: [
// External
D.ThirdParty.Amplitude,
],
settings: .settings(
configurations: IdleConfiguration.presentationConfigurations
)
),
]
)
1 change: 1 addition & 0 deletions project/Projects/Module/Logger/Resources/empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// AccountRegisterationLogBuilder.swift
// Logger
//
// Created by choijunios on 10/30/24.
//

import Foundation

public struct AccountRegisterationLogObject: LoggingObject {

public var eventType: String = "AccountRegister"

public var properties: [String : Any] {
[
"step": step,
"stepName": stepName
]
}

let step: Int
let stepName: String

init(step: Int, stepName: String) {
self.step = step
self.stepName = stepName
}
}

public class AccountRegisterationLogBuilder: LogObjectBuilder {

let step: Int
let stepName: String

public init(step: Int, stepName: String) {
self.step = step
self.stepName = stepName
}

public func build() -> LoggingObject {
AccountRegisterationLogObject(step: step, stepName: stepName)
}
}
49 changes: 49 additions & 0 deletions project/Projects/Module/Logger/Sources/AmplitudeLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// AmplitudeLogger.swift
// Logger
//
// Created by choijunios on 10/30/24.
//

import Foundation
import Combine


import AmplitudeSwift

public class AmplitudeLogger: Logger {

private let objectPublisher: PassthroughSubject<LoggingObject, Never> = .init()

private var bag: Set<AnyCancellable> = .init()

private let amplitude: Amplitude

public init() {

self.amplitude = Amplitude(
configuration: Configuration(
apiKey: AmplitudeConfig.apiKey
)
)

objectPublisher
.throttle(for: 0.3, scheduler: DispatchQueue.main, latest: true)
.sink { [weak self] object in

let eventType = object.eventType
let eventProperties = object.properties

self?.amplitude.track(eventType: eventType, eventProperties: eventProperties)
}
.store(in: &bag)
}

public func setUserId(id: String) {
amplitude.setUserId(userId: id)
}

public func send(_ object: any LoggingObject) {
objectPublisher.send(object)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// CreatePostLogBuilder.swift
// Logger
//
// Created by choijunios on 10/30/24.
//

import Foundation

public struct CreatePostLogObject: LoggingObject {

public var eventType: String = "CreatePost"

public var properties: [String : Any] {
[
"step": step,
"stepName": stepName
]
}

let step: Int
let stepName: String

init(step: Int, stepName: String) {
self.step = step
self.stepName = stepName
}
}

public class CreatePostLogBuilder: LogObjectBuilder {

let step: Int
let stepName: String

public init(step: Int, stepName: String) {
self.step = step
self.stepName = stepName
}

public func build() -> LoggingObject {
CreatePostLogObject(step: step, stepName: stepName)
}
}
14 changes: 14 additions & 0 deletions project/Projects/Module/Logger/Sources/LogObjectBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// LogObjectBuilder.swift
// Logger
//
// Created by choijunios on 10/31/24.
//

import Foundation

public protocol LogObjectBuilder {

/// 로깅 오브젝트를 생성한다.
func build() -> LoggingObject
}
20 changes: 20 additions & 0 deletions project/Projects/Module/Logger/Sources/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Logger.swift
// Logger
//
// Created by choijunios on 10/30/24.
//

import Foundation


public protocol LoggingObject {
var eventType: String { get }
var properties: [String: Any] { get }
}

public protocol Logger {

func send(_ object: LoggingObject)
}

17 changes: 17 additions & 0 deletions project/Projects/Module/Logger/Sources/MockLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MockLogger.swift
// Logger
//
// Created by choijunios on 10/30/24.
//

import Foundation

public class MockLogger: Logger {

public init() { }

public func send(_ object: any LoggingObject) {
print("[\(object.eventType)] \(object.properties)")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class MultiLineTextField: UITextView {
placeHolderLabel.topAnchor.constraint(equalTo: frameGuide.topAnchor, constant: textContainerInset.top),
placeHolderLabel.leftAnchor.constraint(equalTo: frameGuide.leftAnchor, constant: textContainerInset.left),
placeHolderLabel.rightAnchor.constraint(equalTo: frameGuide.rightAnchor, constant: -textContainerInset.right),

placeHolderLabel.bottomAnchor.constraint(equalTo: frameGuide.bottomAnchor, constant: -textContainerInset.bottom),
])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
guard let windowScene = scene as? UIWindowScene else { return }

DependencyInjector.shared.assemble(MockAssemblies)
DependencyInjector.shared.register(CenterRegisterLogger.self, CenterAuthLogger())

authCoordinator = .init()

Expand Down Expand Up @@ -63,12 +62,3 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
authCoordinator?.start()
}
}

class CenterAuthLogger: CenterRegisterLogger {

func logCenterRegisterStep(stepName: String, stepIndex: Int) { }

func startCenterRegister() { }

func logCenterRegisterDuration() { }
}
Loading
Loading