Skip to content

Instantly share code, notes, and snippets.

@dictav
Created December 18, 2014 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dictav/d108736d1008109598da to your computer and use it in GitHub Desktop.
Save dictav/d108736d1008109598da to your computer and use it in GitHub Desktop.
Logger for Swift
//
// Logger.swift
//
// The MIT License (MIT)
//
// Copyright (c) 2014 dictav
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
enum Logger: Printable{
case Debug,Info,Warning,Error,Fatal
var description: String {
switch self {
case .Debug: return "Debug"
case .Info: return "Info"
case .Warning: return "Warning"
case .Error: return "Error"
case .Fatal: return "Fatal"
}
}
static var logger: String -> () = println
static var dateFormatter: NSDateFormatter = {
let f = NSDateFormatter()
f.dateFormat = "yyyy-mm-dd HH:MM:ss.SSS z"
return f
}()
private static func _log(m: String, lv: Logger, f: String, fn: String, l: Int) -> String{
let date = Logger.dateFormatter.stringFromDate(NSDate())
let text = "\(fn.lastPathComponent):L\(l):\(f) \n → [\(date)] \(lv.description)> \(m)"
return text
}
static func debug(message: String, f: String = __FUNCTION__, fn: String = __FILE__, l: Int = __LINE__) {
#if DEBUG
logger(_log(message, lv: Logger.Debug, f:f, fn:fn, l:l))
#endif
}
static func info(message: String, f: String = __FUNCTION__, fn: String = __FILE__, l: Int = __LINE__) {
logger(_log(message, lv: Logger.Info, f:f, fn:fn, l:l))
}
static func warning(message: String, f: String = __FUNCTION__, fn: String = __FILE__, l: Int = __LINE__) {
logger(_log(message, lv: Logger.Warning, f:f, fn:fn, l:l))
}
static func error(message: String, f: String = __FUNCTION__, fn: String = __FILE__, l: Int = __LINE__) {
logger(_log(message, lv: Logger.Error, f:f, fn:fn, l:l))
}
static func fatal(message: String, f: String = __FUNCTION__, fn: String = __FILE__, l: Int = __LINE__) {
logger(_log(message, lv: Logger.Fatal, f:f, fn:fn, l:l))
abort()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment