Skip to content

Commit

Permalink
[CAT-44] Logger 추가 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
devMinseok authored Jul 19, 2024
1 parent 2f6ec99 commit b0dffad
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum PomoNyang {
public enum Shared: Modulable {
case DesignSystem
case Utils
case Logger
}
}

Expand Down
14 changes: 14 additions & 0 deletions Projects/Shared/Logger/Project.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ProjectDescription
import ProjectDescriptionHelpers

@_spi(PomoNyang)
import DependencyPlugin

let project: Project = .project(
module: PomoNyang.Shared.Logger,
scripts: [],
targets: [
.sources
],
dependencies: []
)
64 changes: 64 additions & 0 deletions Projects/Shared/Logger/Sources/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Logger.swift
// Logger
//
// Created by devMinseok on 7/19/24.
// Copyright © 2024 PomoNyang. All rights reserved.
//

import OSLog

public enum LogCategory: String {
case network = "Network"
case ui = "UI"
case database = "Database"
case analytics = "Analytics"
case performance = "Performance"
case etc = "ETC"
case web = "WEB"
}

public final class Logger {
public static let shared = Logger()

public private(set) var logs: [String] = []

private init() {}

#if DEBUG
private let appIdentifier = Bundle.main.bundleIdentifier ?? "???"
private let logsAccessQueue = DispatchQueue(label: "com.kimcaddie.logger.logsAccessQueue", attributes: .concurrent)

private func logger(category: LogCategory) -> os.Logger {
return os.Logger(subsystem: appIdentifier, category: category.rawValue)
}

public func log(level: OSLogType = .default, category: LogCategory = .etc, _ string: @autoclosure () -> String) {
let logMessage = string()
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
print("\(logMessage)")
} else {
self.logger(category: category).log(level: level, "\(logMessage)")
}
appendLog(logMessage)
}

public func clear() {
logsAccessQueue.async(flags: .barrier) {
self.logs = []
}
}

private func appendLog(_ logMessage: String) {
logsAccessQueue.async(flags: .barrier) {
self.logs.append(logMessage)
}
}
#else
@inlinable @inline(__always)
public func log(level: OSLogType = .default, category: LogCategory = .etc, _ string: @autoclosure () -> String) {}

@inlinable @inline(__always)
public func clear() {}
#endif
}

0 comments on commit b0dffad

Please sign in to comment.