From 12eb2374de80da376af7da99f8d86b8685464dfa Mon Sep 17 00:00:00 2001 From: Subeom Choi Date: Tue, 23 Jan 2024 13:47:51 +0900 Subject: [PATCH] feature: add isPaginateLogEnabled to SabyAppleLogger --- .../Alias/{Logger => }/LoggerProtocol.swift | 0 .../AppleLogger/Constant/LoggerConstant.swift | 27 +++++++++++ Source/AppleLogger/Data/LogLevel.swift | 10 ++--- Source/AppleLogger/Data/LoggerSetting.swift | 12 ++++- Source/AppleLogger/Implement/LogService.swift | 45 ++++++++++++++----- Source/AppleLogger/Implement/OSLogger.swift | 4 +- .../AppleLogger/Implement/PrintLogger.swift | 4 +- Test/AppleLogger/LoggerTest.swift | 2 + 8 files changed, 82 insertions(+), 22 deletions(-) rename Source/AppleLogger/Alias/{Logger => }/LoggerProtocol.swift (100%) create mode 100644 Source/AppleLogger/Constant/LoggerConstant.swift diff --git a/Source/AppleLogger/Alias/Logger/LoggerProtocol.swift b/Source/AppleLogger/Alias/LoggerProtocol.swift similarity index 100% rename from Source/AppleLogger/Alias/Logger/LoggerProtocol.swift rename to Source/AppleLogger/Alias/LoggerProtocol.swift diff --git a/Source/AppleLogger/Constant/LoggerConstant.swift b/Source/AppleLogger/Constant/LoggerConstant.swift new file mode 100644 index 0000000..49bbc29 --- /dev/null +++ b/Source/AppleLogger/Constant/LoggerConstant.swift @@ -0,0 +1,27 @@ +// +// LoggerConstant.swift +// SabyAppleLogger +// +// Created by WOF on 1/23/24. +// + +import Foundation + +struct LoggerConstant { + private static let paginateSize = 10 + + static let paginatedLog = { (message: String) in + stride(from: 0, to: message.count, by: paginateSize).map { point in + let start = message.index( + message.startIndex, + offsetBy: point + ) + let end = message.index( + message.startIndex, + offsetBy: point + paginateSize, + limitedBy: message.endIndex + ) ?? message.endIndex + return String(message[start.. Void + ) { + if setting.isPaginateLogEnabled { + let logs = LoggerConstant.paginatedLog(message) + let id = arc4random() % 1000000000 + + logs.enumerated().forEach { (index, log) in + printBlock( + "\(log)" + + "\nlog={page=\(index + 1)/\(logs.count), id=\(id)}" + ) + } + return + } + + printBlock(message) + } +} + public struct OSLogService: LogService { - let osLog: OSLog + public let setting: LoggerSetting public func log(level: LogLevel, _ message: String) { - self.sendLog("%s", log: self.osLog, type: level.osLogType, message) - } - - private func sendLog(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg) { - os_log(message, log: log, type: type, args) - } - - init(_ osLog: OSLog) { - self.osLog = osLog + log(level: level, message) { + os_log("%{public}s", log: setting.osLog, type: level.osLogType, $0) + } } } public struct PrintLogService: LogService { + public let setting: LoggerSetting + public func log(level: LogLevel, _ message: String) { - print("[AirBridge : \(level.name)] \(message)") + let message = "[\(setting.subsystem)/\(setting.category)/\(level.name)] \(message)" + log(level: level, message) { + print($0) + } } } diff --git a/Source/AppleLogger/Implement/OSLogger.swift b/Source/AppleLogger/Implement/OSLogger.swift index 37be07a..c2fa3b5 100644 --- a/Source/AppleLogger/Implement/OSLogger.swift +++ b/Source/AppleLogger/Implement/OSLogger.swift @@ -15,12 +15,12 @@ public final class OSLogger: LoggerType { public init(_ subsystem: String, category: String) { let setting = LoggerSetting(subsystem: subsystem, category: category) self.loggerSetting = setting - self.logService = OSLogService(setting.osLog) + self.logService = OSLogService(setting: setting) } public init(setting: LoggerSetting) { self.loggerSetting = setting - self.logService = OSLogService(setting.osLog) + self.logService = OSLogService(setting: setting) } } diff --git a/Source/AppleLogger/Implement/PrintLogger.swift b/Source/AppleLogger/Implement/PrintLogger.swift index 248983e..1d0136e 100644 --- a/Source/AppleLogger/Implement/PrintLogger.swift +++ b/Source/AppleLogger/Implement/PrintLogger.swift @@ -13,12 +13,12 @@ public final class PrintLogger: LoggerType { public init(_ subsystem: String, category: String) { self.loggerSetting = LoggerSetting(subsystem: subsystem, category: category) - self.logService = PrintLogService() + self.logService = PrintLogService(setting: loggerSetting) } public init(setting: LoggerSetting) { self.loggerSetting = setting - self.logService = PrintLogService() + self.logService = PrintLogService(setting: loggerSetting) } } diff --git a/Test/AppleLogger/LoggerTest.swift b/Test/AppleLogger/LoggerTest.swift index 8b898ae..2fa0788 100644 --- a/Test/AppleLogger/LoggerTest.swift +++ b/Test/AppleLogger/LoggerTest.swift @@ -83,6 +83,8 @@ fileprivate var defaultSetting: LoggerSetting { // MARK: - Mocks for test fileprivate class MockLogService: LogService { + let setting = defaultSetting + let expectation: XCTestExpectation? func log(level: SabyAppleLogger.LogLevel, _ message: String) {