The code is:
let redColor = \"\\u{001B}[0;31m\"
var message = \"Some Message\"
print(redColor + message) //This doesn\'t work
print(\"\\(redColor)\\(message
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 += "
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
Nowadays, Xcode debugging console doesn't support coloring.
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
As @LeslieGodwin mentioned, the XcodeColors Xcode plugin adds color support to the Xcode console (for Xcode versions below 8)