Custom Logging

The built in logging property is a Logger which is initialized with a default LoggerBlockType. When a message is sent to the log property, this block is executed on queue.

Therefore logging to a 3rd party logger is supported by setting the default logger block. In addition to the message to log, the block also receives the severity, the file path, function and line number. Therefore it is should be possible to any 3rd party logger.

As an example, here we will show integration with SwiftyBeaver, which is taken from the Permissions example project.

import Operations
import SwiftyBeaver

// This is following SwiftBeaver's example
let log = SwiftyBeaver.self

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Set up Logging with SwiftyBeaver
        let console = ConsoleDestination()
        log.addDestination(console)

        LogManager.logger = { message, severity, file, function, line in
            switch severity {
            case .Verbose:
                log.verbose(message, file, function, line: line)
            case .Notice:
                log.debug(message, file, function, line: line)
            case .Info:
                log.info(message, file, function, line: line)
            case .Warning:
                log.warning(message, file, function, line: line)
            case .Fatal:
                log.error(message, file, function, line: line)
            }
        }
        
        LogManager.severity = .Info

        return true
    }
}

Did this page help you?