Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 59 additions & 14 deletions Poppool/CoreLayer/Infrastructure/Infrastructure/Logger/Logger.swift
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
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 사용되는 변수가 내부에서만 사용되는것 같은데 private로 감싸주거나 이 변수를 필요로하는 메서드 내부에서 let으로 선언해줘도 좋아보입니다 ☺️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private으로 따뜻하게 감싸줬습니다..


private static var loggers: [String: os.Logger] = [:]
Copy link
Member

Choose a reason for hiding this comment

The 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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로거를 생성할 때 의미하는 카테고리와 로그를 남길 때 파라미터로 받는 카테고리에 대해 혼용이 되고있는것 같아요!

제가 간단하게 공부한바로는 Logger를 생성할 때 사용되는 카테고리는 "debug", "release"와 같은 지금 사용되는 로거가 어느 상황(앱을 개발할때 또는 배포된 앱에서의 기록을 위해)에서 남길 기록들을 보관할것인지 결정하는 용도이고, Logger.log() 메서드에서 활용되는 카테고리는 해당 로그가 어떤 용도인지 "debug", "network", "event"와 같은 것을 표현하는 용도로 구분되는 것으로 확인했어요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 확인했습니다
보다 사용할때 수월하도록
카테고리와 레벨정도로 분리해서 수정하겠습니다

Copy link
Contributor Author

@zzangzzangguy zzangzzangguy Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스크린샷 2025-04-30 오후 8 03 53

사진과 같이 level로 분리하여 사용할수있도록 수정되었습니다


public static func log(
message: Any,
category: Level,
fileName: String = "Input is not found",
line: Int? = nil
fileName: String = #file,
line: Int = #line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이걸 별도로 주입해주는 케이스가 있는지 잘 생각이 안나서 불필요한 파라미터라 생각이 들어요!!

혹시 해당 파라미터가 필요한 예시를 알 수 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존의 디버그창 print를 개선하기 위해 도입한 로거라서 프린트 해주는 부분이 없는것이 좋아보여요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

콘솔창에서만 확인할수있도록 수정하겠습니다!

}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 이미지 파일이 이번 로거 작업이랑은 연관이 없어 보이는데 제거좀 해주실 수 있을까요?

그리고 이미지 추가하실때 svg로 추출하고 single scale로 적용해주시는게 이미지의 크기가 변경되는 상황에서 유리합니다!

지금 다른 작업의 내용에서 방금 말씀드린 svg 변경 적용해서 작업하시면 좋을것 같아요 ☺️

Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"images" : [
{
"filename" : "solid.png",
"filename" : "solid 복사본 2.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "solid 복사본.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "solid.png",
"idiom" : "universal",
"scale" : "3x"
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"scale" : "1x"
},
{
"filename" : "solid 복사본.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "solid 복사본 2.png",
"idiom" : "universal",
"scale" : "3x"
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.