Last active
April 30, 2019 04:23
-
-
Save nil-biribiri/3d4cf9854055e0a58c2fd66947d921f3 to your computer and use it in GitHub Desktop.
OSlog Wrapper class
This file contains 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
import os.log | |
/// Wrapper class for os_log function | |
public class Logger: NSObject { | |
/// Returns the default singleton instance. | |
static let shared = Logger() | |
/// Application layer description | |
/// | |
/// - app: Application layer log | |
/// - ui: User Experience layer log | |
/// - network: Networking layer log | |
/// - database: Database layer log | |
/// - analytic: Analytic layer log | |
enum Category: String { | |
case app, ui, network, database, analytic | |
} | |
/// Log accessibility level | |
/// | |
/// - `public`: Log message will be visible in Console app | |
/// - `private`: Log message won't be visible in Console app | |
enum AccessLevel: String { | |
case `public`, `private` | |
} | |
/// Returns current thread name | |
private var currentThread: String { | |
if Thread.isMainThread { | |
return "main" | |
} else { | |
if let threadName = Thread.current.name, !threadName.isEmpty { | |
return"\(threadName)" | |
} else if let queueName = String(validatingUTF8: __dispatch_queue_get_label(nil)), !queueName.isEmpty { | |
return"\(queueName)" | |
} else { | |
return String(format: "%p", Thread.current) | |
} | |
} | |
} | |
/// Creates OSLog object which describes log subsystem and category | |
/// | |
/// - Parameter category: Category, provided predefined log category | |
/// - Returns: OSLog | |
@available(iOS 10.0, *) | |
private func createOSLog(category: Category) -> OSLog { | |
return OSLog(subsystem: Bundle.main.bundleIdentifier ?? "-", category: category.rawValue.uppercased()) | |
} | |
/// Prints provided log message with help of os_log function | |
/// | |
/// - Parameters: | |
/// - category: Category, provided predefined log category | |
/// - access: AccessLevel, log access level (default is private) | |
/// - type: OSLogType, log type level, for example, .debug, .info, .error (default is info) | |
/// - message: String, provided log message | |
@available(iOS 10.0, *) | |
func log(category: Logger.Category, | |
message: String, | |
access: Logger.AccessLevel = Logger.AccessLevel.private, | |
type: OSLogType = OSLogType.info, | |
fileName: String = #file, | |
functionName: String = #function, | |
lineNumber: Int = #line) { | |
let file = (fileName as NSString).lastPathComponent | |
let line = String(lineNumber) | |
switch access { | |
case .private: | |
os_log("[%{private}@] [%{private}@:%{private}@ %{private}@] > %{private}@", log: createOSLog(category: category), type: type, currentThread, file, line, functionName, message) | |
case .public: | |
os_log("[%{public}@] [%{public}@:%{public}@ %{public}@] > %{public}@", log: createOSLog(category: category), type: type, currentThread, file, line, functionName, message) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment