Swift - 自定义日志输出debugLog


1、封装方法

// MARK: - 封装的日志输出功能(T表示不指定日志信息参数类型)
func debugLog(_ 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)