+

Search Tips | Advanced Search

For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.


Using Logger in Swift projects

In order to use OCLogger in Swift projects, we 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 Develop native applications for iOS in Xcode.
  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)
        }
      }

  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:

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: Develop native applications for iOS in Xcode