Skip to content

Instantly share code, notes, and snippets.

@rnkyr
Last active October 30, 2020 16:15
Show Gist options
  • Select an option

  • Save rnkyr/c3366b8746b425bedbcedaf3d21b8580 to your computer and use it in GitHub Desktop.

Select an option

Save rnkyr/c3366b8746b425bedbcedaf3d21b8580 to your computer and use it in GitHub Desktop.
//
// Logger.swift
//
// Created by Roman Kyrylenko on 5/25/17.
// Copyright Β© 2017 pr0ctopus. All rights reserved.
//
import Foundation
#if canImport(Firebase)
import Firebase
#endif
enum LogLevel: Int {
case test, verbose, success, warn, fail
func glyph() -> String {
switch self {
case .test: return "πŸ’œ"
case .verbose: return "πŸ’™"
case .success: return "πŸ’š"
case .warn: return "πŸ’›"
case .fail: return "πŸ’”"
}
}
}
public enum Logger {
static let currentLogLevel = LogLevel.test
public static func success(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) {
log(.success, string, file: file, line: line)
}
public static func error(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) {
log(.fail, string, file: file, line: line)
}
public static func test(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) {
log(.test, string, file: file, line: line)
}
public static func verbose(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) {
log(.verbose, string, file: file, line: line)
}
public static func warn(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) {
log(.warn, string, file: file, line: line)
}
private static func log(_ level: LogLevel, _ string: () -> String, file: String = #file, line: Int = #line) {
#if DEBUG
if currentLogLevel.rawValue > level.rawValue {
return
}
let startIndex = file.range(of: "/", options: .backwards)?.upperBound
let fileName = file[startIndex!...]
print(" \(level.glyph()) [\(NSDate())] \(fileName)(\(line)) | \(string())")
#elseif canImport(Firebase)
if level.rawValue >= LogLevel.warn.rawValue {
let startIndex = file.range(of: "/", options: .backwards)?.upperBound
let fileName = file[startIndex!...]
Crashlytics.crashlytics().log(" \(level.glyph()) [\(NSDate())] \(fileName)(\(line)) | \(string())")
}
#endif
}
}
extension Logger {
public static func error(_ error: Error, file: String = #file, line: Int = #line) {
self.error("\(error)", file: file, line: line)
}
public static func warn(_ error: Error, file: String = #file, line: Int = #line) {
self.warn("\(error)", file: file, line: line)
}
}
extension Logger {
static func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) {
#if DEBUG
Swift.assert(condition(), message(), file: file, line: line)
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment