From 41e4039d30054b75e31e82171eecb9928e300ad2 Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 14:47:04 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20[#1121]=20scheme?= =?UTF-8?q?=20=EC=9D=B8=EC=9E=90=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=20=EB=94=94=ED=85=8C=EC=9D=BC/=EA=B0=84=EC=86=8C?= =?UTF-8?q?=ED=99=94=20=EB=B6=84=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Logging/CustomLoggingPlugin.swift | 63 ++++++++++++++----- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index 7403fb91f..c084ca82c 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -1,19 +1,21 @@ -// -// CustomLoggingPlugin.swift -// BaseDomain -// -// Created by KTH on 2024/03/04. -// Copyright © 2024 yongbeomkwak. All rights reserved. -// - import Foundation import Moya +import OSLog #if DEBUG + fileprivate enum NetworkLogLevel: String { + case short + case detailed + } public final class CustomLoggingPlugin: PluginType { - public init() {} - + let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: "NETWORK") + private let logLevel: NetworkLogLevel + + public init() { + self.logLevel = CustomLoggingPlugin.getLogLevelFromArguments() ?? .detailed + } + public func willSend(_ request: RequestType, target: TargetType) { guard let httpRequest = request.request else { print("--> 유효하지 않은 요청") @@ -30,9 +32,16 @@ import Moya log.append("\(bodyString)\n") } log.append("---------------- END \(method) -----------------------\n") - print(log) + + switch logLevel { + case .short: + let log = "[🛜 Request] [\(method)] [\(target)] \(url)" + logger.log(level: .debug, "\(log)") + case .detailed: + logger.log(level: .debug, "\(log)") + } } - + public func didReceive(_ result: Result, target: TargetType) { switch result { case let .success(response): @@ -41,7 +50,7 @@ import Moya onFail(error, target: target) } } - + func onSuceed(_ response: Response, target: TargetType, isFromError: Bool) { let request = response.request let url = request?.url?.absoluteString ?? "nil" @@ -56,9 +65,17 @@ import Moya log.append("\(reString)\n") } log.append("------------------- END HTTP (\(response.data.count)-byte body) -------------------\n") - print(log) + + switch logLevel { + case .short: + let log = "[🛜 Response] [\(statusCode)] [\(target)] \(url)" + logger.log(level: .debug, "\(log)") + case .detailed: + logger.log(level: .debug, "\(log)") + } + } - + func onFail(_ error: MoyaError, target: TargetType) { if let response = error.response { onSuceed(response, target: target, isFromError: true) @@ -68,7 +85,21 @@ import Moya log.append("<-- \(error.errorCode) \(target)\n") log.append("\(error.failureReason ?? error.errorDescription ?? "unknown error")\n") log.append("<-- END HTTP\n") - print(log) + + logger.log("\(log)") + } + } + + extension CustomLoggingPlugin { + /// Scheme Arguments에서 로그 레벨을 가져오는 함수 + /// Arguments Passed On Launch 에 "-networkLogLevel detailed" 또는 "-networkLogLevel short" 입력 + private static func getLogLevelFromArguments() -> LogLevel? { + let arguments = ProcessInfo.processInfo.arguments + if let logLevelIndex = arguments.firstIndex(of: "-networkLogLevel"), logLevelIndex + 1 < arguments.count { + let logLevelValue = arguments[logLevelIndex + 1] + return LogLevel(rawValue: logLevelValue) + } + return nil } } From 0c14d727a27ebdeb104c6b471fa6c30d83c04f3d Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 14:53:59 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20[#1121]=20enum?= =?UTF-8?q?=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseDomain/Sources/Logging/CustomLoggingPlugin.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index c084ca82c..41af18642 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -93,11 +93,11 @@ import OSLog extension CustomLoggingPlugin { /// Scheme Arguments에서 로그 레벨을 가져오는 함수 /// Arguments Passed On Launch 에 "-networkLogLevel detailed" 또는 "-networkLogLevel short" 입력 - private static func getLogLevelFromArguments() -> LogLevel? { + private static func getLogLevelFromArguments() -> NetworkLogLevel? { let arguments = ProcessInfo.processInfo.arguments if let logLevelIndex = arguments.firstIndex(of: "-networkLogLevel"), logLevelIndex + 1 < arguments.count { let logLevelValue = arguments[logLevelIndex + 1] - return LogLevel(rawValue: logLevelValue) + return NetworkLogLevel(rawValue: logLevelValue) } return nil } From 7b488753da4b50fa8708a4f39d7656d79f8357da Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 15:09:34 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20[#1121]=20argumen?= =?UTF-8?q?t=20->=20=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Logging/CustomLoggingPlugin.swift | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index 41af18642..d6f197a3c 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -5,7 +5,7 @@ import OSLog #if DEBUG fileprivate enum NetworkLogLevel: String { case short - case detailed + case detail } public final class CustomLoggingPlugin: PluginType { @@ -13,7 +13,7 @@ import OSLog private let logLevel: NetworkLogLevel public init() { - self.logLevel = CustomLoggingPlugin.getLogLevelFromArguments() ?? .detailed + self.logLevel = CustomLoggingPlugin.getLogLevelFromArguments() ?? .detail } public func willSend(_ request: RequestType, target: TargetType) { @@ -37,7 +37,7 @@ import OSLog case .short: let log = "[🛜 Request] [\(method)] [\(target)] \(url)" logger.log(level: .debug, "\(log)") - case .detailed: + case .detail: logger.log(level: .debug, "\(log)") } } @@ -70,7 +70,7 @@ import OSLog case .short: let log = "[🛜 Response] [\(statusCode)] [\(target)] \(url)" logger.log(level: .debug, "\(log)") - case .detailed: + case .detail: logger.log(level: .debug, "\(log)") } @@ -91,15 +91,12 @@ import OSLog } extension CustomLoggingPlugin { - /// Scheme Arguments에서 로그 레벨을 가져오는 함수 - /// Arguments Passed On Launch 에 "-networkLogLevel detailed" 또는 "-networkLogLevel short" 입력 + /// Environment Variables 에서 로그 레벨을 가져오는 함수 + /// Environment Variables 에 key : NETWORK_LOG_LEVEL, value : shrot 또는 detail private static func getLogLevelFromArguments() -> NetworkLogLevel? { - let arguments = ProcessInfo.processInfo.arguments - if let logLevelIndex = arguments.firstIndex(of: "-networkLogLevel"), logLevelIndex + 1 < arguments.count { - let logLevelValue = arguments[logLevelIndex + 1] - return NetworkLogLevel(rawValue: logLevelValue) - } - return nil + guard let logLevelValue = ProcessInfo.processInfo.environment["NETWORK_LOG_LEVEL"] else { return nil } + guard let networkLogLevel = NetworkLogLevel(rawValue: logLevelValue) else { return nil } + return networkLogLevel } } From 7c8a86b617be27473cd608c2213065ce6056b5e6 Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 15:12:42 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20[#1121]=20?= =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseDomain/Sources/Logging/CustomLoggingPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index d6f197a3c..6c151eed7 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -91,7 +91,7 @@ import OSLog } extension CustomLoggingPlugin { - /// Environment Variables 에서 로그 레벨을 가져오는 함수 + /// Environment Variables 에서 로그 레벨을 가져오는 메소드 /// Environment Variables 에 key : NETWORK_LOG_LEVEL, value : shrot 또는 detail private static func getLogLevelFromArguments() -> NetworkLogLevel? { guard let logLevelValue = ProcessInfo.processInfo.environment["NETWORK_LOG_LEVEL"] else { return nil } From 54ba595753f5b081d980942c9548bb9f7be01def Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 15:12:46 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=F0=9F=8E=A8=20::=20=EC=BD=94=EB=93=9C=20Fo?= =?UTF-8?q?rmatting=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Logging/CustomLoggingPlugin.swift | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index 6c151eed7..f92fde676 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -11,11 +11,11 @@ import OSLog public final class CustomLoggingPlugin: PluginType { let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: "NETWORK") private let logLevel: NetworkLogLevel - + public init() { self.logLevel = CustomLoggingPlugin.getLogLevelFromArguments() ?? .detail } - + public func willSend(_ request: RequestType, target: TargetType) { guard let httpRequest = request.request else { print("--> 유효하지 않은 요청") @@ -32,7 +32,7 @@ import OSLog log.append("\(bodyString)\n") } log.append("---------------- END \(method) -----------------------\n") - + switch logLevel { case .short: let log = "[🛜 Request] [\(method)] [\(target)] \(url)" @@ -41,7 +41,7 @@ import OSLog logger.log(level: .debug, "\(log)") } } - + public func didReceive(_ result: Result, target: TargetType) { switch result { case let .success(response): @@ -50,7 +50,7 @@ import OSLog onFail(error, target: target) } } - + func onSuceed(_ response: Response, target: TargetType, isFromError: Bool) { let request = response.request let url = request?.url?.absoluteString ?? "nil" @@ -65,7 +65,7 @@ import OSLog log.append("\(reString)\n") } log.append("------------------- END HTTP (\(response.data.count)-byte body) -------------------\n") - + switch logLevel { case .short: let log = "[🛜 Response] [\(statusCode)] [\(target)] \(url)" @@ -73,9 +73,8 @@ import OSLog case .detail: logger.log(level: .debug, "\(log)") } - } - + func onFail(_ error: MoyaError, target: TargetType) { if let response = error.response { onSuceed(response, target: target, isFromError: true) @@ -85,7 +84,7 @@ import OSLog log.append("<-- \(error.errorCode) \(target)\n") log.append("\(error.failureReason ?? error.errorDescription ?? "unknown error")\n") log.append("<-- END HTTP\n") - + logger.log("\(log)") } } From b75f6ea7e57245fb64b26b33223d01b2313f252e Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 15:25:07 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20[#1121]=20tuist?= =?UTF-8?q?=20=EC=84=B8=ED=8C=85=20=EC=95=B1=20=EC=8A=A4=ED=82=B4=EC=97=90?= =?UTF-8?q?=20=ED=99=98=EA=B2=BD=20=EB=B3=80=EC=88=98=20=EA=B8=B0=EB=B3=B8?= =?UTF-8?q?=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Projects/App/Project.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Projects/App/Project.swift b/Projects/App/Project.swift index e51acd313..e035c20a7 100644 --- a/Projects/App/Project.swift +++ b/Projects/App/Project.swift @@ -58,7 +58,8 @@ let targets: [Target] = [ .debug(name: .debug, xcconfig: "XCConfig/Secrets.xcconfig"), .release(name: .release, xcconfig: "XCConfig/Secrets.xcconfig") ] - ) + ), + environmentVariables: ["NETWORK_LOG_LEVEL" : "short"] ), .target( name: "\(env.name)Tests", From 04cd2a9dd31a54b0df9d1daef66a6e45d2defcfa Mon Sep 17 00:00:00 2001 From: youn9k Date: Wed, 14 Aug 2024 15:25:10 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=F0=9F=8E=A8=20::=20=EC=BD=94=EB=93=9C=20Fo?= =?UTF-8?q?rmatting=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Projects/App/Project.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projects/App/Project.swift b/Projects/App/Project.swift index e035c20a7..0f553aec0 100644 --- a/Projects/App/Project.swift +++ b/Projects/App/Project.swift @@ -59,7 +59,7 @@ let targets: [Target] = [ .release(name: .release, xcconfig: "XCConfig/Secrets.xcconfig") ] ), - environmentVariables: ["NETWORK_LOG_LEVEL" : "short"] + environmentVariables: ["NETWORK_LOG_LEVEL": "short"] ), .target( name: "\(env.name)Tests", From 37a273670e8a1221cf4c9a9cd94367a256d30670 Mon Sep 17 00:00:00 2001 From: Youngkyu Song Date: Wed, 14 Aug 2024 16:00:13 +0900 Subject: [PATCH 8/9] Update Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift Co-authored-by: baegteun --- .../BaseDomain/Sources/Logging/CustomLoggingPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index f92fde676..c3b8b05e2 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -91,7 +91,7 @@ import OSLog extension CustomLoggingPlugin { /// Environment Variables 에서 로그 레벨을 가져오는 메소드 - /// Environment Variables 에 key : NETWORK_LOG_LEVEL, value : shrot 또는 detail + /// Scheme의 Environment Variables 에 key : NETWORK_LOG_LEVEL, value : short 또는 detail private static func getLogLevelFromArguments() -> NetworkLogLevel? { guard let logLevelValue = ProcessInfo.processInfo.environment["NETWORK_LOG_LEVEL"] else { return nil } guard let networkLogLevel = NetworkLogLevel(rawValue: logLevelValue) else { return nil } From 339c73f2e7908742c3ef284409b9f388636d17cb Mon Sep 17 00:00:00 2001 From: Youngkyu Song Date: Wed, 14 Aug 2024 16:00:21 +0900 Subject: [PATCH 9/9] Update Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift Co-authored-by: baegteun --- .../BaseDomain/Sources/Logging/CustomLoggingPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift index c3b8b05e2..3a9c94d07 100644 --- a/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift +++ b/Projects/Domains/BaseDomain/Sources/Logging/CustomLoggingPlugin.swift @@ -3,7 +3,7 @@ import Moya import OSLog #if DEBUG - fileprivate enum NetworkLogLevel: String { + private enum NetworkLogLevel: String { case short case detail }