美文网首页
swift--自定义LOG

swift--自定义LOG

作者: 陈水寒 | 来源:发表于2017-06-24 09:47 被阅读23次

将此代码放到AppDelegate.swift中就可以了,会自动变成全局可以访问的函数

    func GGLog<T>(message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
        
        #if DEBUG
        
            let fileName = (file as NSString).lastPathComponent
            
            print("\(fileName):[\(funcName)](\(lineNum))-\(message)")
            
        #endif
    }

自定义了logger级别

enum MPLogLevel : Int {
    case debug       = 0
    case info        = 5
    case warning     = 10
    case error       = 15
    
    public func name() -> String {
        var readableName: String
        switch (self) {
        case .debug:
            readableName = "D"
        case .info:
            readableName = "I"
        case .warning:
            readableName = "W"
        case .error:
            readableName = "E"
        }
        return readableName
    }
}

func GGLog<T>(level: MPLogLevel, message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {

    #if DEBUG
        let fileName = (file as NSString).lastPathComponent.split(separator: ".").first!
        print("[\(level.name())]: \(fileName)✽[\(funcName)]✽(\(lineNum))🏀 \(message)")
    #endif
}

相关文章

网友评论

      本文标题:swift--自定义LOG

      本文链接:https://www.haomeiwen.com/subject/dbahcxtx.html