-
Notifications
You must be signed in to change notification settings - Fork 1
[Feat] 네트워킹 로깅 플러그인 구현 #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Projects/DataSource/Sources/NetworkService/Plugin/BitnagilLoggingPlugin.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // | ||
| // BitnagilLoggingPlugin.swift | ||
| // DataSource | ||
| // | ||
| // Created by 이동현 on 8/3/25. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Shared | ||
|
|
||
| struct BitnagilLoggingPlugin: NetworkPlugin { | ||
| func willSend(request: URLRequest, endpoint: Endpoint) async throws -> URLRequest { | ||
| let urlString = request.url?.absoluteString ?? "" | ||
| let method = request.httpMethod ?? "" | ||
| let headers = request.allHTTPHeaderFields ?? [:] | ||
| let body: String | ||
| if let bodyData = request.httpBody { | ||
| body = String(data: bodyData, encoding: .utf8) ?? "디코딩 실패" | ||
| } else { | ||
| body = "내용 없음" | ||
| } | ||
|
|
||
| BitnagilLogger.log( | ||
| logType: .debug, | ||
| message: """ | ||
| REQUEST➡️ | ||
| - URL: \(urlString) | ||
| - Method: \(method) | ||
| - Headers: \(headers) | ||
| - Body: \(body) | ||
| """ | ||
| ) | ||
| return request | ||
| } | ||
|
|
||
| func didReceive(response: URLResponse, data: Data?, endpoint: Endpoint) async throws { | ||
| let urlString = response.url?.absoluteString ?? "" | ||
|
|
||
| guard let httpResponse = response as? HTTPURLResponse else { | ||
| BitnagilLogger.log(logType: .error, message: "🚨\(urlString)에 대한 response는 HTTPResponse가 아닙니다.🚨") | ||
| return | ||
| } | ||
|
|
||
| let statusCode = httpResponse.statusCode | ||
|
|
||
| // code/message 추출 | ||
| var code: String = "" | ||
| var message: String = "" | ||
| if | ||
| let data = data, | ||
| let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] | ||
| { | ||
| code = json["code"] as? String ?? "" | ||
| message = json["message"] as? String ?? "" | ||
| } | ||
|
|
||
| BitnagilLogger.log( | ||
| logType: .debug, | ||
| message: """ | ||
| RESPONSE⬅️ | ||
| - 요청URL: \(urlString) | ||
| - Status: \(statusCode) | ||
| - Code: \(code) | ||
| - Message: \(message) | ||
| """ | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
보안 민감 정보 로깅에 대한 주의가 필요합니다.
현재 코드는 모든 헤더 정보를 로깅하고 있어 토큰과 같은 민감한 정보가 노출될 수 있습니다. 검색된 학습 내용에 따르면 choijungp님이 디버깅 목적으로 토큰 값 로깅을 선호하신다고 하지만, 프로덕션 환경에서는 보안상 위험할 수 있습니다.
다음과 같이 민감한 헤더를 마스킹하는 방식을 고려해보세요:
또는 디버그 빌드에서만 전체 헤더를 로깅하도록 할 수 있습니다:
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OSLogger의 log level이 debug라 release 환경에서는 로그가 찍히지 않는다 생각했습니다. 추후 확인해보고 잘못된 지식이었다면 조치하도록 하겠습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
💡 Result:
In iOS, OSLogger (using the OSLog framework) supports multiple log levels, including debug, info, and error. The key behavior difference is that debug-level logs generated by OSLogger are visible in Debug builds but are suppressed in Release builds by default. This design helps prevent leaking sensitive or verbose debug information in production and reduces performance and storage overhead[1][3][7].
Supporting Details:
Debug Logs in Debug Builds:
Debug logs (e.g.,
Logger().debug("Debug log")) are fully visible when running Debug builds, such as in the Xcode debugger or Console app, provided filtering options for log levels are enabled[1][7]. This is intended for development and troubleshooting during active coding.Debug Logs in Release Builds:
In Release builds (used for App Store distribution and TestFlight), debug-level logs are not emitted or are filtered out by the unified logging system. This means you generally cannot see debug-level output from OSLogger in production builds, even if you search directly for them in the Console on a device[1][3][7].
debugandtracemessages from release builds to avoid unnecessary performance impact, storage use, and potential privacy issues[1][3].error,fault) do remain visible in Release builds, as these are intended for significant events and errors that should be monitored in production[1][7].Best Practices and Rationale:
#if DEBUG) to allow more verbose logging in development and automatically silence or reduce log output in production[3][4].info,error, or higher levels, depending on severity. Alternatively, you may create a custom build configuration, but this is typically discouraged unless necessary for debugging specific production issues[2][4].Summary Table:
References:
If you require debug-level log information in production for troubleshooting, consider elevating affected log statements to the
infoorerrorlevel (temporarily), or, as a last resort, implement a custom logging mechanism with explicit configuration to ensure it does not violate user privacy or App Store requirements[2][3][4].Citations:
@taipaise 맞습니다! 웹 검색 결과 확인해보니 당신의 이해가 정확합니다.
OSLogger의 debug 레벨 로그는 실제로 Release 빌드에서 자동으로 억제됩니다:
이는 Apple이 의도적으로 설계한 보안 및 성능 최적화 기능입니다. 따라서 현재 구현이 보안상 안전하다고 볼 수 있습니다.
다만 코드의 명확성을 위해 이런 동작에 대한 주석을 추가하는 것을 고려해볼 수 있습니다:
정확한 지식을 가지고 계셨고, 확인해보겠다는 열린 자세도 훌륭합니다! 🐰
🧠 Learnings used