Using Logger in Swift Projects
In order to use OCLogger in Swift projects, you can configure your Swift application by following the steps described in this section.
Procedure
- Create a native MobileFirst application for iOS that uses Swift. For more information, see Developing native applications for iOS.
- In the Xcode IDE, create a Swift file and name it
OCLoggerSwiftExtension.swift. Figure 1. New Xcode FileFigure 2. Swift File TypeFigure 3. Name a Swift File
- Add the following code to the file:
import Foundation extension OCLogger { //Log methods with no metadata func logTraceWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_TRACE, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } func logDebugWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_DEBUG, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } func logInfoWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_INFO, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } func logWarnWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_WARN, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } func logErrorWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_ERROR, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } func logFatalWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_FATAL, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } func logAnalyticsWithMessages(message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_ANALYTICS, message: message, args:getVaList(args), userInfo:Dictionary<String, String>()) } //Log methods with metadata func logTraceWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_TRACE, message: message, args:getVaList(args), userInfo:userInfo) } func logDebugWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_DEBUG, message: message, args:getVaList(args), userInfo:userInfo) } func logInfoWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_INFO, message: message, args:getVaList(args), userInfo:userInfo) } func logWarnWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_WARN, message: message, args:getVaList(args), userInfo:userInfo) } func logErrorWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_ERROR, message: message, args:getVaList(args), userInfo:userInfo) } func logFatalWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_FATAL, message: message, args:getVaList(args), userInfo:userInfo) } func logAnalyticsWithUserInfo(userInfo:Dictionary<String, String>, message:String, _ args: CVarArgType...) { logWithLevel(OCLogger_ANALYTICS, message: message, args:getVaList(args), userInfo:userInfo) } } //Alternatives for macros private func _metadataDictionary(file:String, fn:String, line:Int) -> Dictionary<String, String> { return [ "$method" : fn, "$file" : file.lastPathComponent, "$line" : String(line), "$src": "swift" ]; } public func OCLoggerTrace(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_TRACE, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); } public func OCLoggerDebug(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_DEBUG, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); } public func OCLoggerInfo(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_INFO, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); } public func OCLoggerWarn(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_WARN, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); } public func OCLoggerError(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_ERROR, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); } public func OCLoggerFatal(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_FATAL, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); } public func OCLoggerAnalytics(message:String, package:String = "IMF", file: String = __FILE__, fn: String = __FUNCTION__, line: Int = __LINE__, #args: CVarArgType...) { OCLogger.getInstanceWithPackage(package).logWithLevel(OCLogger_ANALYTICS, message: message, args:getVaList(args), userInfo: _metadataDictionary(file, fn, line)); }
- Test that the Swift extension is working by using the Logger
in Swift with the following code:
OCLogger.setLevel(OCLogger_TRACE) OCLogger.setCapture(true); let logger : OCLogger = OCLogger.getInstanceWithPackage("MyTestLoggerPackage") logger.logTraceWithMessages("Hello %@", "Trace"); logger.logTraceWithMessages("Hello Trace"); OCLoggerDebug("Hello Debug!"); OCLoggerDebug("Hello %@", package: "SomePackageName", args: "Debug!");
Results
SwiftHelloWorld[7591:4265124] [TRACE] [MyTestLoggerPackage] Hello Trace
SwiftHelloWorld[7591:4265124] [TRACE] [MyTestLoggerPackage] Hello Trace
SwiftHelloWorld[7591:4265124] [DEBUG] [IMF] viewDidLoad() in ViewController.swift:26 :: Hello Debug!
SwiftHelloWorld[7591:4265124] [DEBUG] [SomePackageName] viewDidLoad() in ViewController.swift:27 :: Hello Debug!
Note: The
project name used in this example is SwiftHelloWorld.
Your project name will show in the output in place of SwiftHelloWorld.
The code was added to the viewDidLoad() function
in the ViewController.swift file. Your
output depends on where you added the code in your project.
The timestamps were removed from this example for clarity.
Parent topic: Developing native applications for iOS