Swift3.x - CustomLog

2017-03-22  本文已影响18人  ibabyblue

自定义Log
在开发中打印一些信息,对于调试程序是必然的!当然这是指在Debug的情况下,在Release情况下,就不需要打印了!
下面我们自定义Log输出:

//MARK:--自定义Log
func CustomLog<T>(message: T, fileName: String = #file, funcName: String = #function, lineNumber: Int = #line){
    let fileNameComponent = (fileName as NSString).lastPathComponent
    print("__FILE__:\(fileNameComponent) __LINE__:\(lineNumber) __INFO__:\(message)")
}

注意:

如何设置自定义Log的Debug和Release模式
设置如图:

设置Debug标志

上图中的步骤4中的标志格式为:-D 你的标志,其中"你的标志"可以随便设置字符传,但是需要你设置的简明扼要!不能省略!
然后自定义的Log可以这样写:

//MARK:-自定义Log
func CustomLog<T>(message: T, fileName: String = #file, funcName: String = #function, lineNumber: Int = #line){
    #if DEBUG
    let fileNameComponent = (fileName as NSString).lastPathComponent
    print("__FILE__:\(fileNameComponent) __LINE__:\(lineNumber) __INFO__:\(message)")
    #endif
}

OK!这样设置之后,在Debug模式下,会有Log,在Release模式下,就不会有Log啦!
注意:推荐将此自定义Log方法,定义在AppDelegate.swift文件中,并且在类方法的外面定义!
Zeb

上一篇 下一篇

猜你喜欢

热点阅读