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

๐Ÿ”€ :: (#1121) ๋„คํŠธ์›Œํฌ ๋กœ๊ทธ๋ฅผ ๋””ํ…Œ์ผ/๊ฐ„์†Œํ™” ๋กœ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ #1122

Merged
merged 10 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Projects/App/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//
// CustomLoggingPlugin.swift
// BaseDomain
//
// Created by KTH on 2024/03/04.
// Copyright ยฉ 2024 yongbeomkwak. All rights reserved.
//

import Foundation
import Moya
import OSLog

#if DEBUG
private enum NetworkLogLevel: String {
case short
case detail
}

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() ?? .detail
}

public func willSend(_ request: RequestType, target: TargetType) {
guard let httpRequest = request.request else {
Expand All @@ -30,7 +32,14 @@ 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 .detail:
logger.log(level: .debug, "\(log)")
}
}

public func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
Expand All @@ -56,7 +65,14 @@ 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 .detail:
logger.log(level: .debug, "\(log)")
}
}

func onFail(_ error: MoyaError, target: TargetType) {
Expand All @@ -68,7 +84,18 @@ 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 {
/// Environment Variables ์—์„œ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์†Œ๋“œ
/// 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 }
return networkLogLevel
}
}

Expand Down
Loading