代码是:
let redColor = "\u{001B}[0;31m"
var message = "Some Message"
print(redColor + message) //This doesnt work
print("\(redColor)\(message)" //This also doesnt work
,输出如下:
[0;31mSome Message
我还阅读了这篇文章:Color ouput with Swift command line tool,它似乎无法发挥作用。
我不想使用图书馆。
答案 0 :(得分:16)
如今,Xcode调试控制台不支持着色。
答案 1 :(得分:2)
从Xcode 8开始,Xcode不支持控制台着色。
但是,由于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
答案 2 :(得分:2)
添加到@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")
答案 3 :(得分:0)
正如@LeslieGodwin所提到的,XcodeColors Xcode插件为Xcode控制台添加了颜色支持。
答案 4 :(得分:-1)
添加我自己的贡献:
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”更改图标
答案 5 :(得分:-1)
如果您正在 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..