美文网首页
Swift - 自定义日志输出debugLog

Swift - 自定义日志输出debugLog

作者: 麦志超 | 来源:发表于2017-09-23 11:45 被阅读0次

    1、封装方法

    // MARK: - 封装的日志输出功能(T表示不指定日志信息参数类型)
    func debugLog<T>(_ message: T, file: String = #file, function: String = #function, line: Int = #line)
    {
        #if DEBUG
            //获取文件名
            let fileName = (file as NSString).lastPathComponent
            // 创建一个日期格式器
            let dformatter = DateFormatter()
            // 为日期格式器设置格式字符串
            dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            // 使用日期格式器格式化当前日期、时间
            let datestr = dformatter.string(from: Date())
            //打印日志内容
            print("***** log start *****")
            print(datestr)
            print("fileName: \(fileName) -> line: \(line) -> func: \(function)")
            print(message)
            print("*****  log end  *****")
        #endif
    }
    

    2、使用样例

    debugLog("自定义日志输出")

    3、输出

    ***** log start *****
    2017-09-23 11:38:59
    fileName: ViewController.swift -> line: 20 -> func: horizontalShakeButtonTap
    自定义日志输出
    *****  log end  *****
    

    相关文章

      网友评论

          本文标题:Swift - 自定义日志输出debugLog

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