Quick Start

LogKit makes it easy to get started logging!

  1. Install LogKit
  2. import LogKit
  3. Create a Logger

Note: If you installed LogKit by including its source directly within your project, there is no need to import LogKit (in fact, you will not be able to). Skip that step and just create a Logger.

To get started right away, install LogKit and follow the Simple Example code below. Later, it’s easy to add more Endpoints by just modifying the Logger’s initialization. All logging calls will begin outputting to the new Endpoints the next time your application is run.

Simple Example (in iOS)

AppDelegate.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import UIKit
import LogKit

let log = LXLogger()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
    // ...

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

        log.debug("Hello Internet!")

        return true
    }
}

This simple example initializes a Logger with a standard Console Endpoint. Lines 2, 4, and 12 contain the relevant code for getting LogKit set up quickly and making your first Log Entry.

Because the Logger instance log is created as a global, you may now make logging calls from any of your project’s source files.

Advanced Usage

The basics of LogKit don’t change, but its real power comes from using Endpoints other than the console. LogKit allows you to write Log Entries to as many Endpoints as desired. Each of these Endpoints can be customized to log in different formats, or set to accept different Priority Levels.

Complex Example (in OS X) with Multiple Endpoints

AppDelegate.swift

import Cocoa
import LogKit

let log = LXLogger(endpoints: [

    LXLogSerialConsoleEndpoint(
        dateFormatter: {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "HH':'mm':'ss'.'SSS"
            dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
            return dateFormatter
        }(),
        entryFormatter: { entry in
            return "\(entry.dateTime) [\(entry.logLevel.uppercaseString)] \(entry.message)"
        }
    ),

    LXLogFileEndpoint(
        fileURL: (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) as? [NSURL])?.first?
            .URLByAppendingPathComponent("logs", isDirectory: true)
            .URLByAppendingPathComponent("log.txt"),
        minimumLogLevel: .Notice,
        entryFormatter: { entry in
            return "\(entry.dateTime) (\(entry.timestamp)) [\(entry.logLevel.uppercaseString)] {thread: \(entry.threadID) '\(entry.threadName)' main: \(entry.isMainThread)} \(entry.functionName) <\(entry.fileName):\(entry.lineNumber).\(entry.columnNumber)> \(entry.message)"
        }
    ),

])

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    // ...

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        log.info("Hello, Internet...")
        log.notice("... do you hear me?")

    }
}

This complex example initializes a Logger with several Endpoints:

These are just two of several Endpoints that LogKit includes. See the Endpoint documentation for other Endpoints that LogKit includes, and for information on creating your own Endpoints.