Created
March 27, 2019 23:15
-
-
Save mendesbarreto/ffa7756395010b4d197e5da8b93d2afe to your computer and use it in GitHub Desktop.
PrettySwiftLogger
This file contains hidden or 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 Crashlytics | |
extension Crashlytics: LogRecorder { | |
func record(log: String, reason _: String? = nil) { | |
CLSLogv("%@", getVaList([log])) | |
} | |
func record(errorString: String, reason: String? = nil) { | |
recordCustomExceptionName(errorString, reason: reason, frameArray: []) | |
} | |
func record(error: Error, withAdditionalUserInfo info: [String: Any]? = nil) { | |
recordError(error, withAdditionalUserInfo: info) | |
} | |
} |
This file contains hidden or 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 | |
protocol LogRecorder { | |
func record(log: String, reason: String?) | |
func record(errorString: String, reason: String?) | |
func record(error: Error, withAdditionalUserInfo info: [String: Any]?) | |
} |
This file contains hidden or 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 Crashlytics | |
import Foundation | |
final class PrettySwiftLogger { | |
private var logLevel: LogLevel = .verbose | |
static let shared = PrettySwiftLogger() | |
static let dateFormat = "yyyy-MM-dd hh:mm:ssSSS" | |
static var dateFormatter: DateFormatter { | |
let formatter = DateFormatter() | |
formatter.dateFormat = dateFormat | |
formatter.locale = Locale.current | |
formatter.timeZone = TimeZone.current | |
return formatter | |
} | |
private var logRecorder: LogRecorder? | |
private init() {} | |
func printDebug(object: Any, | |
fileName: String, | |
line: Int, | |
column: Int, | |
functionName: String, | |
logHeader: String, | |
logLevel: LogLevel) { | |
let logMessage = "[\(Date().asStringLog())] \(logHeader) [\(sourceFileName(filePath: fileName))]:\(line) \(column) \(functionName) 👉 \(object)" | |
log(header: logHeader, message: logMessage, level: logLevel) | |
} | |
private func log(header: String, message: String, level: LogLevel) { | |
guard shouldLogOnTerminalBy(level: level) else { return } | |
print(message) | |
guard let logRecorder = logRecorder else { return } | |
if level.contains(.critical) || level.contains(.error) { | |
logRecorder.record(errorString: message, reason: header) | |
} else { | |
logRecorder.record(log: message, reason: header) | |
} | |
} | |
private func sourceFileName(filePath: String) -> String { | |
let components = filePath.components(separatedBy: "/") | |
return components.isEmpty ? "" : components.last! | |
} | |
func shouldLogOnTerminalBy(level: LogLevel) -> Bool { | |
return logLevel.contains(level) | |
} | |
func update(logLevelTo level: LogLevel) { | |
logLevel = level | |
} | |
func add(logRecorder recorder: LogRecorder) { | |
logRecorder = recorder | |
} | |
func removeLogecorder() { | |
logRecorder = nil | |
} | |
} | |
private extension Date { | |
func asStringLog() -> String { | |
return PrettySwiftLogger.dateFormatter.string(from: self as Date) | |
} | |
} | |
func debugPrintError(_ object: Any, | |
fileName: String = #file, | |
line: Int = #line, | |
column: Int = #column, | |
functionName: String = #function) { | |
PrettySwiftLogger.shared.printDebug(object: object, | |
fileName: fileName, | |
line: line, | |
column: column, | |
functionName: functionName, | |
logHeader: LogHeaders.errorEmoji, | |
logLevel: .error) | |
} | |
func debugPrintDebug(_ object: Any, | |
fileName: String = #file, | |
line: Int = #line, | |
column: Int = #column, | |
functionName: String = #function) { | |
PrettySwiftLogger.shared.printDebug(object: object, | |
fileName: fileName, | |
line: line, | |
column: column, | |
functionName: functionName, | |
logHeader: LogHeaders.debugEmoji, | |
logLevel: .debug) | |
} | |
func debugPrintInfo(_ object: Any, | |
fileName: String = #file, | |
line: Int = #line, | |
column: Int = #column, | |
functionName: String = #function) { | |
PrettySwiftLogger.shared.printDebug(object: object, | |
fileName: fileName, | |
line: line, | |
column: column, | |
functionName: functionName, | |
logHeader: LogHeaders.informationEmoji, | |
logLevel: .information) | |
} | |
func debugPrintWarning(_ object: Any, fileName: String = #file, | |
line: Int = #line, | |
column: Int = #column, | |
functionName: String = #function) { | |
PrettySwiftLogger.shared.printDebug(object: object, | |
fileName: fileName, | |
line: line, | |
column: column, | |
functionName: functionName, | |
logHeader: LogHeaders.warningEmoji, | |
logLevel: .waning) | |
} | |
func debugPrintCritical(_ object: Any, fileName: String = #file, | |
line: Int = #line, | |
column: Int = #column, | |
functionName: String = #function) { | |
PrettySwiftLogger.shared.printDebug(object: object, | |
fileName: fileName, | |
line: line, | |
column: column, | |
functionName: functionName, | |
logHeader: LogHeaders.criticalEmoji, | |
logLevel: .critical) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment