print() to console log with color

后端 未结 5 1555
情书的邮戳
情书的邮戳 2020-12-17 10:00

The code is:

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


        
相关标签:
5条回答
  • 2020-12-17 10:42

    Adding my own contribution:

    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 += "                                                                    
    0 讨论(0)
  • 2020-12-17 10:43

    Xcode doesn't support console coloring since Xcode 8.

    But Since Xcode is fully unicode compatible, you can use emojis instead! for example you can use You can use ⚠️ for warning messages and

    0 讨论(0)
  • 2020-12-17 10:44

    Nowadays, Xcode debugging console doesn't support coloring.

    0 讨论(0)
  • 2020-12-17 10:44

    Adding to @Mojtaba's answer, you can use this for automating logging:

    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                                                                    
    0 讨论(0)
  • 2020-12-17 10:55

    As @LeslieGodwin mentioned, the XcodeColors Xcode plugin adds color support to the Xcode console (for Xcode versions below 8)

    0 讨论(0)
提交回复
热议问题