Home

Awesome

KZLinkedConsole

Ever wondered which part of your application logged the message you just saw in console?

Wonder no more, instead just click on it to jump to the culprit. Simple as that.

Installation

Download the source, build the Xcode project and restart Xcode. The plugin will automatically be installed in ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins. To uninstall, just remove the plugin from there (and restart Xcode).

Swift 3

Xcode8 / Swift3 is on swift3 branch.

Alcatraz

This plugin can be installed using Alcatraz. Search for KZLinkedConsole in Alcatraz.

Details

If a console logs a fileName.extension:123 that name turns into a clickable hyperlink that will open the specific file and highlight the line.

That way you can either use your own logging mechanism and just add this simple prefix, e.g.

func logMessage(message: String, filename: String = __FILE__, line: Int = __LINE__, function: String = __FUNCTION__) {
    print("\((filename as NSString).lastPathComponent):\(line) \(function):\r\(message)")
}

Integration with popular loggers

QorumLogs.KZLinkedConsoleSupportEnabled = true

Swift version (Objective-C version is part of KZBootstrap):

import Foundation
import CocoaLumberjack.DDDispatchQueueLogFormatter

class KZFormatter: DDDispatchQueueLogFormatter {

  lazy var formatter: NSDateFormatter = {
      let dateFormatter = NSDateFormatter()
      dateFormatter.formatterBehavior = .Behavior10_4
      dateFormatter.dateFormat = "HH:mm:ss.SSS"
      return dateFormatter
  }()

  override func formatLogMessage(logMessage: DDLogMessage!) -> String {
      let dateAndTime = formatter.stringFromDate(logMessage.timestamp)

      var logLevel: String
      let logFlag = logMessage.flag
      if logFlag.contains(.Error) {
          logLevel = "ERR"
      } else if logFlag.contains(.Warning){
          logLevel = "WRN"
      } else if logFlag.contains(.Info) {
          logLevel = "INF"
      } else if logFlag.contains(.Debug) {
          logLevel = "DBG"
      } else if logFlag.contains(.Verbose) {
          logLevel = "VRB"
      } else {
          logLevel = "???"
      }

      let formattedLog = "\(dateAndTime) |\(logLevel)| \((logMessage.file as NSString).lastPathComponent):\(logMessage.line): ( \(logMessage.function) ): \(logMessage.message)"
      return formattedLog;
  }
}

Opening files from outside Xcode

KZLinkedConsole also supports opening files from outside Xcode through distributed notifications. The xed command line tool is supposed to provide this feature but is unfortunately terribly broken.

In order to open the file Example.m and select the line 123, simply do the following:

[[NSDistributedNotificationCenter defaultCenter]
    postNotificationName:@"pl.pixle.KZLinkedConsole.OpenFile"
                  object:@"Example.m"
                userInfo:@{ @"Line": @123 }
      deliverImmediately:YES];

You can also specify an absolute path if you know it. The userInfo dictionary with line number information is optional.

If the file was successfully opened, KZLinkedConsole will post back a pl.pixle.KZLinkedConsole.DidOpenFile distributed notification.

More info

Read more about creation of this plugin on my blog

Follow me on twitter for more useful iOS stuff