'print() to console log with color

The code is:

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

and the output would look like this:

[0;31mSome Message

I've also read this post: Color ouput with Swift command line tool, and it doesn't seem to work.

I don't want to use libraries.



Solution 1:[1]

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 ? for error messages. (like the Xcode itself)

Or simply use these note books as a color:

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

for example:

print("??", "Touch is not disabled as expected")

? Bones

Using this method will help you to find the logs in the source code as fast as ?? by a simple eye scan:

Demo

And you can search for them ?? to let the Xcode take you there. Take a look at this result comparison:

Custom emoji search

emoji search

vs

Word search

word search

Solution 2:[2]

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? 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")
        }
    }

}

You can use it this way:

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

Solution 3:[3]

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

Solution 4:[4]

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 += "?"
        case .warning:
            logMessage += "?"
        case .error:
            logMessage += "?"
        }
        
        let fileName = file.components(separatedBy: "/").last ?? ""
        logMessage += " \(fileName) -> LINE: \(line) -> \(function) => \(message)"
        
        print(logMessage)
        return logMessage
    }

}

You can always change the icons using "crtl+cmd+space"

Solution 5:[5]

If you're looking for a replacement color logging within Xcode see this new swift-log feature that I created.

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

It uses the new standard swift-log compatible library.

The output will look like this if passing in .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..

The output will look like this if passing in .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..

Solution 6:[6]

Xcode console color theme/scheme

Go to Preferences > Fonts and Colors
Select Console at top of window
Then select the color for Executable console output

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mojtaba Hosseini
Solution 3 Mojtaba Hosseini
Solution 4
Solution 5 Nick N
Solution 6