-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] #127 OSLog도입 #128
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
[FEAT] #127 OSLog도입 #128
Changes from 2 commits
ec3f787
09d15eb
a04857f
8f3e675
4444ed3
e014ccd
b5a134f
93592c9
2c17a4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import Foundation | ||
| import OSLog | ||
|
|
||
| public struct Logger { | ||
| private static let subsystem = Bundle.main.bundleIdentifier ?? "com.poppoolIOS.poppool" | ||
|
|
||
| public enum Level { | ||
| case info | ||
| case debug | ||
|
|
@@ -42,28 +45,70 @@ public struct Logger { | |
| return "🍎" | ||
| } | ||
| } | ||
|
|
||
| var osLogType: OSLogType { | ||
| switch self { | ||
| case .debug: | ||
| return .debug | ||
| case .info, .event: | ||
| return .info | ||
| case .network: | ||
| return .default | ||
| case .error: | ||
| return .error | ||
| case .custom: | ||
| return .default | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static var isShowFileName: Bool = false | ||
| static var isShowLine: Bool = false | ||
| static var isShowFileName: Bool = false // 파일 이름 포함여부 | ||
| static var isShowLine: Bool = true // 라인 번호 포함 여부 | ||
| static var isShowLog: Bool = true | ||
|
|
||
| private static var loggers: [String: os.Logger] = [:] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로거의 Key를 String으로 하기보단 enum 타입으로 하면 휴먼 에러도 방지하고 사용단계에서도 편할것 같습니다!! 👍🏻 |
||
| private static func getLogger(for category: Level) -> os.Logger { | ||
| let categoryName = category.categoryName | ||
|
|
||
| if let cachedLogger = loggers[categoryName] { | ||
| return cachedLogger | ||
| } | ||
|
|
||
| let logger = os.Logger(subsystem: subsystem, category: categoryName) | ||
| loggers[categoryName] = logger | ||
| return logger | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로거를 생성할 때 의미하는 카테고리와 로그를 남길 때 파라미터로 받는 카테고리에 대해 혼용이 되고있는것 같아요! 제가 간단하게 공부한바로는 Logger를 생성할 때 사용되는 카테고리는 "debug", "release"와 같은 지금 사용되는 로거가 어느 상황(앱을 개발할때 또는 배포된 앱에서의 기록을 위해)에서 남길 기록들을 보관할것인지 결정하는 용도이고, Logger.log() 메서드에서 활용되는 카테고리는 해당 로그가 어떤 용도인지 "debug", "network", "event"와 같은 것을 표현하는 용도로 구분되는 것으로 확인했어요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 확인했습니다
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| public static func log( | ||
| message: Any, | ||
| category: Level, | ||
| fileName: String = "Input is not found", | ||
| line: Int? = nil | ||
| fileName: String = #file, | ||
| line: Int = #line | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이걸 별도로 주입해주는 케이스가 있는지 잘 생각이 안나서 불필요한 파라미터라 생각이 들어요!! 혹시 해당 파라미터가 필요한 예시를 알 수 있을까요?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이전에 질문주셨던 로거 위치처럼 편의상 등록해뒀기때문에 필요하지않는다고 다들 생각하신다면 디폴트로 처리해둬도 괜찮습니다! |
||
| ) { | ||
| if isShowLog { | ||
| print("\(category.categoryIcon) [\(category.categoryName)]: \(message)") | ||
| if isShowFileName { | ||
| guard let fileName = fileName.components(separatedBy: "/").last else { return } | ||
| print(" \(category.categoryIcon) [FileName]: \(fileName)") | ||
| } | ||
| if isShowLine { | ||
| guard let line = line else { return } | ||
| print(" \(category.categoryIcon) [Line]: \(line)") | ||
| } | ||
| guard isShowLog else { return } | ||
|
|
||
| let logger = getLogger(for: category) | ||
| var fullMessage = "\(category.categoryIcon) \(message)" | ||
|
|
||
| if isShowFileName { | ||
| guard let fileNameOnly = fileName.components(separatedBy: "/").last else { return } | ||
| fullMessage += " | 📁 \(fileNameOnly)" | ||
| } | ||
|
|
||
| if isShowLine { | ||
| fullMessage += " | 📍 \(line)" | ||
| } | ||
|
|
||
| logger.log(level: category.osLogType, "\(fullMessage, privacy: .public)") | ||
|
|
||
| // 디버깅 시 Xcode 콘솔에서도 바로 확인할 수 있도록 print도 함께 사용 불필요시 제거 | ||
| print("\(category.categoryIcon) [\(category.categoryName)]: \(message)") | ||
| if isShowFileName { | ||
| guard let fileNameOnly = fileName.components(separatedBy: "/").last else { return } | ||
| print(" \(category.categoryIcon) [FileName]: \(fileNameOnly)") | ||
| } | ||
| if isShowLine { | ||
| print(" \(category.categoryIcon) [Line]: \(line)") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존의 디버그창 print를 개선하기 위해 도입한 로거라서 프린트 해주는 부분이 없는것이 좋아보여요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 콘솔창에서만 확인할수있도록 수정하겠습니다! |
||
| } | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 이미지 파일이 이번 로거 작업이랑은 연관이 없어 보이는데 제거좀 해주실 수 있을까요? 그리고 이미지 추가하실때 svg로 추출하고 single scale로 적용해주시는게 이미지의 크기가 변경되는 상황에서 유리합니다! 지금 다른 작업의 내용에서 방금 말씀드린 svg 변경 적용해서 작업하시면 좋을것 같아요 |

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.
여기서 사용되는 변수가 내부에서만 사용되는것 같은데 private로 감싸주거나 이 변수를 필요로하는 메서드 내부에서 let으로 선언해줘도 좋아보입니다☺️
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.
private으로 따뜻하게 감싸줬습니다..