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

  1. Create a native MobileFirst application for iOS that uses Swift. For more information, see Developing native applications for iOS.
  2. In the Xcode IDE, create a Swift file and name it OCLoggerSwiftExtension.swift.
    Figure 1. New Xcode File
    This screen capture shows how to use the File menu to create a file.
    Figure 2. Swift File Type
    This screen capture shows how to select a Swift File.
    Figure 3. Name a Swift File
    This screen capture shows how to name the Swift file.
  3. 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));
    }
  4. 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

Verify the output is similar to the following output:
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.