import Foundation
class Logger {
private var className:String
private let prefix = "★★★★★"
private let prefixInfo = "====="
private let currentLever = 6
private static let LEVEL_FATAL = 1
private static let LEVEL_ERROR = 2
private static let LEVEL_WARN = 3
private static let LEVEL_INFO = 4
private static let LEVEL_DEBUG = 5
private static let LEVEL_TRACE = 6
private static let NUM_1 = 1
private static let NUM_2 = 2
private static let SPACE = " "
private static let spot = "."
init(className:String) {
self.className = className
}
func logTrace(strList:String...) {
if currentLever >= Logger.LEVEL_TRACE {
let tempMessage = getMessage(level: " Trace:" ,strList: strList)
print(tempMessage)
}
}
func logDebug(strList:String...) {
if currentLever >= Logger.LEVEL_DEBUG {
let tempMessage = getMessage(level: " Debug:" ,strList: strList)
print(tempMessage)
}
}
func logInfo(strList:String...) {
if currentLever >= Logger.LEVEL_INFO {
let tempMessage = getMessage(level: " Info:" ,strList: strList)
print(tempMessage)
}
}
func logWarn(strList:String...) {
if currentLever >= Logger.LEVEL_WARN {
let tempMessage = getMessage(level: " Warn:" ,strList: strList)
print(tempMessage)
}
}
func logError(strList:String...) {
if currentLever >= Logger.LEVEL_ERROR {
let tempMessage = getMessage(level: " Error:" ,strList: strList)
print(tempMessage)
}
}
func logFatal(strList:String...) {
if currentLever >= Logger.LEVEL_FATAL {
let tempMessage = getMessage(level: " Fatal:" ,strList: strList)
print(tempMessage)
}
}
func getMessage(level:String, strList:[String]) -> String{
var tempPrefix:String
if level.contains("Info") {
tempPrefix = self.prefixInfo
} else {
tempPrefix = self.prefix
}
var tempMessage = tempPrefix + level + className + Logger.spot
if strList.count > Logger.NUM_1 {
tempMessage = tempMessage + strList[0]
var paraArray = [String]()
for i in 0 ..< strList.count{
if i > 0 {
paraArray.append(strList[i])
}
}
tempMessage = String(format: tempMessage, arguments: paraArray)
} else {
for item in strList {
tempMessage.append(item)
tempMessage.append(Logger.SPACE)
}
}
tempMessage.append(tempPrefix)
return tempMessage
}
}
网友评论