[스위프트3] 스위프트 로거steemCreated with Sketch.

in #swift7 years ago

스위프트 개발시 NSLog나 print를 사용하여 로그를 찍는 일이 많은데 위 로거는 사용시 시간, 파일위치, 라인등을 찍어준다.

/// 로그 표현 형식
///
///  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))")
    }
}

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.035
BTC 95967.22
ETH 3310.18
USDT 1.00
SBD 3.15