[스위프트3] 스위프트 로거
스위프트 개발시 NSLog나 print를 사용하여 로그를 찍는 일이 많은데 위 로거는 사용시 시간, 파일위치, 라인등을 찍어준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/// 로그 표현 형식
///
/// Logger.info(message: "init")
///
/// [INFO] = "init" 2017. 2. 3. 오후 11:54:21 viewDidLoad() ViewController.swift:(23)
///
/// Logger.debug(message: "deinit")
///
/// [DEBUG] = "deinit" 2017. 2. 3. 오후 11:54:21 viewDidLoad() ViewController.swift:(25)
class Logger {
class func debug(message: String, function: String = #function, file: String = #file, line: Int = #line) {
Logger.write(loglevel: "[DEBUG]", message: message, function: function, file: file, line: line)
};
class func info(message: String, function: String = #function, file: String = #file, line: Int = #line) {
Logger.write(loglevel: "[INFO]", message: message, function: function, file: file, line: line)
}
class func warning(message: String, function: String = #function, file: String = #file, line: Int = #line) {
Logger.write(loglevel: "[WARNING]", message: message, function: function, file: file, line: line)
};
class func error(message: String, function: String = #function, file: String = #file, line: Int = #line) {
Logger.write(loglevel: "[ERROR]", message: message, function: function, file: file, line: line)
}
class func write(loglevel: String, message: String, function: String, file: String, line: Int) {
let now = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "ko_KR") as Locale!
dateFormatter.timeStyle = .medium
dateFormatter.dateStyle = .medium
let nowdate = dateFormatter.string(from: now as Date)
var filename = file
if let match = filename.range(of: "[^/]*$", options: .regularExpression) {
filename = filename.substring(with: match)
}
print("\(loglevel)\t \(message) \n\(nowdate) \(function) \(filename):(\(line))")
}
}
| cs |
댓글
댓글 쓰기