Swift: format console log

is it possible to create a custom log function?

This is what println looks like by default ("hello world"):

2015-03-04 18:33:55.788 MyApp[12345:671253923] Hello World 

I would like to output something like:

18:33 MyClass > myFunc [line 1] Hello World 
+4
source share
3 answers

First, for a while you can get the current hour and minute as a String:

func printTime()->String{
        let date = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
        let hour = components.hour
        let minutes = components.minute

        return "\(hour):\(minutes)"
}

And for function, etc. you can use swift literal expressions __FILE__ , __FUNCTION__and __LINE__.

But you do not want to install it every time you want to register. So you can do something like this:

func prettyPrint(print: String, file:String = __FILE__, functionName: String = __FUNCTION__, line:Int = __LINE__) {
    println("\(printTime()) \(file) > \(functionName) [line \(line)] \(print)")

}

You call prettyPrintas follows:

prettyPrint("hey")

And you will get the following result:

/Path/To/Your/File/MyClass.swift > hello [line 81] hey

, :

func getFile(path:String = __FILE__)->String{
    var parts =  path.componentsSeparatedByString("/")
    return parts[parts.count-1]
}

, ChikabuZ, , :

let className = NSStringFromClass(self.classForCoder).pathExtension

():

func getFile(path:String = __FILE__)->String{
    var parts =  path.componentsSeparatedByString("/")
    return parts[parts.count-1]
}
func prettyPrint(print: String, functionName: String = __FUNCTION__, line:Int = __LINE__) {
    println("\(printTime()) \(getFile()) > \(functionName) [line \(line)] \(print)")

}

func printTime()->String{
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
    let hour = components.hour
    let minutes = components.minute

    return "\(hour):\(minutes)"
}

:

MyClass.swift > hello [line 81] hey

@emaloney . ,

println() , Apple (ASL).

NSLog

+1

NSObject, :

class MyClass: NSObject
{
    func myFunc()
    {
        myPrint("Hello World")
    }

}

extension NSObject
{
    func myPrint(text: String)
    {
        let timeFormatter = NSDateFormatter()
        timeFormatter.dateStyle = NSDateFormatterStyle.NoStyle
        timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
        let time = timeFormatter.stringFromDate(NSDate())

        let className = NSStringFromClass(self.classForCoder).pathExtension
        let function = __FUNCTION__

        let line = "\(__LINE__)"

        let result = time + " " + className + " > " + function + " " + line + " " + text
        println(result)
    }
}

let myClass = MyClass()

myClass.myFunc()
+1

, , println(), , Apple (ASL).

ASL - , Mac OS iOS, NSLog() ( Mac ). NSLog() ASL, , NSLog(), . , println(), ASL, , - .

NSLog(), : (1) (2) , , .

CleanroomLogger is the new open source open source logging API . If you are familiar with log4j or CocoaLumberjack, then you will understand CleanroomLogger.

You can customize your own output format by providing your own implementation LogFormatterto ensure that your log messages are formatted exactly as you want.

For more information about CleanroomLogger, visit the GitHub project page .

+1
source

All Articles