Skip to content

Instantly share code, notes, and snippets.

@yoichitgy
Created September 16, 2017 06:28
Show Gist options
  • Save yoichitgy/8806a7da3a52d8e6f5207e6a1a11b5a8 to your computer and use it in GitHub Desktop.
Save yoichitgy/8806a7da3a52d8e6f5207e6a1a11b5a8 to your computer and use it in GitHub Desktop.
iOSDC JAPAN 2017 Demo - Log.swift
import Foundation
import os.log
import Crashlytics
final class Log {
private init() {}
static func debug(message: String) {
writeLog(message: message, level: .debug)
}
static func info(message: String) {
writeLog(message: message, level: .info)
CLSLogv("%@", getVaList([message]))
}
static func error(message: String) {
writeLog(message: message, level: .error)
CLSLogv("[Error] %@", getVaList([message]))
}
static func error(error: Error) {
let message = String(reflecting: error)
writeLog(message: message, level: .error)
CLSLogv("[Error] %@", getVaList([message]))
}
static func fatal(message: String, file: String = #file, function: String = #function, line: Int = #line) {
writeLog(message: message, level: .fatal)
let fileName = file.components(separatedBy: "/").last ?? ""
let error = NSError(
domain: "\(fileName):\(function)",
code: line,
userInfo: ["message": message]
)
Crashlytics.sharedInstance().recordError(error)
assertionFailure(message)
}
static func fatal(error: Error) {
let message = String(reflecting: error)
writeLog(message: message, level: .fatal)
Crashlytics.sharedInstance().recordError(error as NSError)
assertionFailure(message)
}
private static func writeLog(message: String, level: Level) {
os_log("%@%@", log: .default, type: .debug, level.description, message)
}
private enum Level: CustomStringConvertible {
case debug, info, error, fatal
var description: String {
switch self {
case .debug: return "⚒"
case .info: return "ℹ️"
case .error: return "❗️"
case .fatal: return "😱"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment