Created
November 3, 2017 01:32
-
-
Save mmtootmm/117f8f746e1f3b17f8b39ba182dd245c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Logger { | |
enum Level: Int { | |
case verbose = 0, debug, info, warning, error, none | |
var tag: String { | |
switch self { | |
case .verbose: return "VERVOSE" | |
case .debug: return "DEBUG" | |
case .info: return "INFO" | |
case .warning: return "WARNING" | |
case .error: return "ERROR" | |
case .none: fatalError("Try to logging `none` level") | |
} | |
} | |
} | |
var minLevel: Level = .warning | |
var dateFormatter: DateFormatter = { | |
let f = DateFormatter() | |
f.dateFormat = "yy/MM/dd|HH:mm:ss.SSS" | |
return f | |
}() | |
func log(level: Level, file: String = #file, function: String = #function, line: Int = #line, _ m: [Any]) { | |
guard minLevel.rawValue <= level.rawValue else { return } | |
let datetime = dateFormatter.string(from: Date()) | |
var place = file.replacingOccurrences(of: "[^/]*/", with: "", options: .regularExpression).split(separator: ".").first! | |
place += ":\(line)" | |
place += "¶\(function)" | |
// Splatting | |
switch m.count { | |
case 0: print(datetime, "\(level.tag)", place) | |
case 1: print(datetime, "\(level.tag)", place, m[0]) | |
case 2: print(datetime, "\(level.tag)", place, m[0], m[1]) | |
case 3: print(datetime, "\(level.tag)", place, m[0], m[1], m[2]) | |
case 4: print(datetime, "\(level.tag)", place, m[0], m[1], m[2], m[3]) | |
case 5: print(datetime, "\(level.tag)", place, m[0], m[1], m[2], m[3], m[4]) | |
default: print(datetime, "\(level.tag)", place, m) | |
} | |
} | |
static var `default`: Logger = Logger() | |
static func verbose(file: String = #file, function: String = #function, line: Int = #line, _ messages: Any...) { | |
self.default.log(level: .verbose, file: file, function: function, line: line, messages) | |
} | |
static func debug(file: String = #file, function: String = #function, line: Int = #line, _ messages: Any...) { | |
self.default.log(level: .debug, file: file, function: function, line: line, messages) | |
} | |
static func info(file: String = #file, function: String = #function, line: Int = #line, _ messages: Any...) { | |
self.default.log(level: .info, file: file, function: function, line: line, messages) | |
} | |
static func warning(file: String = #file, function: String = #function, line: Int = #line, _ messages: Any...) { | |
self.default.log(level: .warning, file: file, function: function, line: line, messages) | |
} | |
static func error(file: String = #file, function: String = #function, line: Int = #line, _ messages: Any...) { | |
self.default.log(level: .error, file: file, function: function, line: line, messages) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment