NSLog在swift中的简单写法

其实实现这个log的动机仅仅是想看到一个规整的控制台,仅此而已。写这供自己copy一下

/// 输出日志
///
/// - info: normal
/// - success: success
/// - error: error
/// - warning: 警告,可不处理的
enum DBLog {
    case info
    case success
    case error
    case warning
}

extension DBLog {

    /// 输出默认info的日志
    static func cat(_ message: T, file: String = #file, method: String = #function, line: Int = #line) {
        self.info.cat(message)
    }

    /// 输入日志,根据类型,区别符号
    ///
    /// - Parameters:
    ///   - message: 显示的信息
    func cat(_ message: T, file: String = #file, method: String = #function, line: Int = #line) {
        
        #if DEBUG
            var log: String
            switch self {
            case .info:
                log = "  - " + "\(message)"
            case .success:
                log = " ✅ - " + "\(message)"
            case .error:
                log = " ❌ - " + "\(message)"
            case .warning:
                log = " ⚠️ - " + "\(message)"
            }
            print("\((file as NSString).lastPathComponent)[\(line)],\(method):\(log)")
        #endif
    }
}

你可能感兴趣的:(NSLog在swift中的简单写法)