【问题标题】:print() to console log with colorprint() 用颜色控制日志
【发布时间】:2017-03-27 19:03:05
【问题描述】:

代码是:

let redColor = "\u{001B}[0;31m"
var message = "Some Message"
print(redColor + message)  //This doesn't work
print("\(redColor)\(message)") //This also doesn't work

输出如下所示:

[0;31mSome Message

我也看过这篇文章:Color ouput with Swift command line tool,但它似乎不起作用。

我不想使用库。

【问题讨论】:

  • 作为mentioned in the linked-to Q&A,当程序在终端窗口中运行时设置颜色有效,但在 Xcode 控制台中
  • 天哪。严重地?。我想我会用表情符号来标记我的日志。
  • 也在这里确认stackoverflow.com/questions/9005769/…"...因为 Xcode 调试控制台似乎不支持着色。"
  • 好的,谢谢。我应该保留这个问题还是应该关闭它?
  • @DanylS 我不会关闭它,但我会把你作为答案,我们永远不知道 Xcode 的下一次迭代是否会发生变化

标签: swift xcode debugging logging colors


【解决方案1】:

Xcode 自 Xcode 8 起不支持控制台着色。

但由于 Xcode 完全兼容 unicode,您可以使用 emojis 代替!例如,您可以使用 ⚠️ 来获取警告消息,? 来获取错误消息。 (就像 Xcode 本身)

或者干脆将这些笔记本用作颜色:

?: error message
?: warning message
?: ok status message
?: action message
?: canceled status message
?: Or anything you like and want to recognize immediately by color

例如:

print("⚠️", "Touch is not disabled as expected")

? 骨头

使用这种方法,通过简单的眼睛扫描,可以帮助您以 ⚡️ 一样快的速度找到源代码中的日志:

您可以搜索它们??,让 Xcode 将您带到那里。看看这个结果对比:

自定义表情符号搜索

单词搜索

【讨论】:

  • 太有创意了!从来没想过!哈哈哈谢谢!
  • 就像一个魅力,即使是在管道上的构建机器! ?
【解决方案2】:

现在,Xcode 调试控制台不支持着色。

【讨论】:

【解决方案3】:

添加到@Mojtaba's answer,您可以使用它来自动记录:

enum LogType: String{
case error
case warning
case success
case action
case canceled
}


class Logger{

 static func log(_ logType:LogType,_ message:String){
        switch logType {
        case LogType.error:
            print("\n? Error: \(message)\n")
        case LogType.warning:
            print("\n? Warning: \(message)\n")
        case LogType.success:
            print("\n? Success: \(message)\n")
        case LogType.action:
            print("\n? Action: \(message)\n")
        case LogType.canceled:
            print("\n? Cancelled: \(message)\n")
        }
    }

}

你可以这样使用它:

Logger.log(.error, "Invalid Credit Information")

【讨论】:

  • 您正在使用特殊的 Unicode 字符。最好将这些作为转义序列提供,以避免代码中出现 Unicode。
  • 很好的答案和很好的解决方案!
【解决方案4】:

正如@LeslieGodwin 提到的,XcodeColors Xcode 插件为 Xcode 控制台添加了颜色支持(适用于低于 8 的 Xcode 版本)

【讨论】:

    【解决方案5】:

    添加我自己的贡献:

    struct Logger {
        /// Type of logs available
        enum LogType: String {
            /// To log a message
            case debug
            /// To log a warning
            case warning
            /// To log an error
            case error
        }
        
        /// Logs a debug message
        /// - Parameter message: Message to log
        /// - Parameter file: File that calls the function
        /// - Parameter line: Line of code from the file where the function is call
        /// - Parameter function: Function that calls the functon
        /// - Returns: The optional message that was logged
        @discardableResult
        static func d(message: String, file: String = #file, line: Int = #line, function: String = #function) -> String{
            return log(type: .debug, message: message, file: file, line: line, function: function)
        }
        
        /// Logs a warning message
        /// - Parameter message: Message to log
        /// - Parameter file: File that calls the function
        /// - Parameter line: Line of code from the file where the function is call
        /// - Parameter function: Function that calls the functon
        /// - Returns: The optional message that was logged
        @discardableResult
        static func w(message: String, file: String = #file, line: Int = #line, function: String = #function) -> String{
            return log(type: .warning, message: message, file: file, line: line, function: function)
        }
        
        /// Logs an error message
        /// - Parameter message: Message to log
        /// - Parameter file: File that calls the function
        /// - Parameter line: Line of code from the file where the function is call
        /// - Parameter function: Function that calls the functon
        /// - Returns: The optional message that was logged
        @discardableResult
        static func e(message: String, file: String = #file, line: Int = #line, function: String = #function) -> String{
            return log(type: .error, message: message, file: file, line: line, function: function)
        }
        
        /// Logs an message
        /// - Parameter logType: Type of message to log
        /// - Parameter message: Message to log
        /// - Parameter file: File that calls the function
        /// - Parameter line: Line of code from the file where the function is call
        /// - Parameter function: Function that calls the functon
        /// - Returns: The optional message that was logged
        @discardableResult
        static func log(type logType: LogType = .debug, message: String, file: String = #file, line: Int = #line, function: String = #function) -> String{
            var logMessage = ""
            
            switch logType{
            case .debug:
                logMessage += "?"
            case .warning:
                logMessage += "?"
            case .error:
                logMessage += "?"
            }
            
            let fileName = file.components(separatedBy: "/").last ?? ""
            logMessage += " \(fileName) -> LINE: \(line) -> \(function) => \(message)"
            
            print(logMessage)
            return logMessage
        }
    
    }
    

    您可以随时使用“crtl+cmd+space”更改图标

    【讨论】:

      【解决方案6】:

      如果您正在 Xcode 中寻找替代颜色记录,请参阅我创建的这个新的 swift-log 功能。

      https://github.com/nneuberger1/swift-log-console-colors

      它使用新的标准 swift-log 兼容库。

      如果传入.cool,输出将如下所示

      2021-05-09T16:13:30-0500 ? debug thingsAboveAdmin : Testing log levels..
      2021-05-09T16:13:30-0500 ℹ️ info thingsAboveAdmin : Testing log levels..
      2021-05-09T16:13:30-0500 ? notice thingsAboveAdmin : Testing log levels..
      2021-05-09T16:13:30-0500 ⚠️ warning thingsAboveAdmin : Testing log levels..
      2021-05-09T16:13:30-0500 ⚡ critical thingsAboveAdmin : Testing log levels..
      2021-05-09T16:13:30-0500 ? error thingsAboveAdmin : Testing log levels..
      

      如果传入.rainbow,输出会是这样的

      2021-05-09T16:17:07-0500 ? debug thingsAboveAdmin : Testing log levels..
      2021-05-09T16:17:07-0500 ? info thingsAboveAdmin : Testing log levels..
      2021-05-09T16:17:07-0500 ? notice thingsAboveAdmin : Testing log levels..
      2021-05-09T16:17:07-0500 ? warning thingsAboveAdmin : Testing log levels..
      2021-05-09T16:17:07-0500 ? critical thingsAboveAdmin : Testing log levels..
      2021-05-09T16:17:07-0500 ? error thingsAboveAdmin : Testing log levels..
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 2015-05-01
        • 1970-01-01
        相关资源
        最近更新 更多