Created
February 25, 2021 12:23
-
-
Save gubikmic/5c8b2e670f247d443b9cf1ec0c0ff8a8 to your computer and use it in GitHub Desktop.
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
// Copyright © 2020 Michael Gubik. All rights reserved. Created on 2020-05-16. | |
import Foundation | |
#if TEST | |
import Sentry | |
#endif | |
// maybe look into os_log: https://stackoverflow.com/a/25951564/11588848 | |
private enum LogLevel: String { | |
case info = "infø" | |
case warn = "warn" | |
case error = "errør" | |
} | |
private var runtimeFingerprint = UUID().uuidString | |
public func log(_ any: Any? = nil, function: String = #function, file: String = #file, line: Int = #line) { | |
log(level: .info, any, function: function, file: file, line: line) | |
} | |
public func logWarn(_ any: Any? = nil, function: String = #function, file: String = #file, line: Int = #line) { | |
log(level: .warn, any, function: function, file: file, line: line) | |
} | |
public func logError(_ any: Any? = nil, function: String = #function, file: String = #file, line: Int = #line) { | |
log(level: .error, any, function: function, file: file, line: line) | |
} | |
public func logSubmit() { | |
#if TEST | |
let event = Event(level: .info) | |
let date = DateFormatter.isoDateFormatter.string(from: Date()) | |
event.message = "operation start - logSubmit - \(date)" | |
event.fingerprint = [runtimeFingerprint] | |
SentrySDK.capture(event: event) | |
#endif | |
} | |
private func log(level: LogLevel, _ any: Any?, function: String, file: String, line: Int) { | |
let logLevel = level | |
let message = String(describing: any ?? "") | |
let time = DateFormatter.debugLogTimeFormatter.string(from: Date()) | |
let file = NSURL(fileURLWithPath: file).lastPathComponent ?? "nil" | |
let level = level.rawValue.uppercased() + emoji(for: level) | |
let sep = message.isEmpty ? "" : ">>" | |
let numLeadingSpaces = message.prefix(while: { $0 == " " }).count | |
let prompt = "[\(level)] \(time) \(function) \(file):\(line)] \(sep) " | |
let paddingForNewlines = String.init(repeating: " ", count: numLeadingSpaces + prompt.count) | |
let indentedMessage = message.replacingOccurrences(of: "\n", with: "\n\(paddingForNewlines)") | |
print("\(prompt)\(indentedMessage)") | |
#if TEST | |
if logLevel == .error { | |
let event = Event(level: .error) | |
event.message = "\(time) \(function) \(file):\(line)] \(sep) \(message)" | |
SentrySDK.capture(event: event) | |
} else { | |
let crumb = Breadcrumb(level: logLevelToSentryLevel(logLevel), category: "") | |
crumb.message = "\(time) \(function) \(file):\(line)] \(sep) \(message)" | |
SentrySDK.addBreadcrumb(crumb: crumb) | |
} | |
#endif | |
} | |
private func emoji(for logLevel: LogLevel) -> String { | |
switch logLevel { | |
case .warn: | |
return "⚠️" | |
case .error: | |
return "🛑" | |
default: | |
return "" | |
} | |
} | |
#if TEST | |
private func logLevelToSentryLevel(_ logLevel: LogLevel) -> SentryLevel { | |
var sentryLevel = SentryLevel.none | |
switch logLevel { | |
case .info: | |
sentryLevel = .info | |
case .warn: | |
sentryLevel = .warning | |
case .error: | |
sentryLevel = .error | |
default: | |
break | |
} | |
return sentryLevel | |
} | |
#endif | |
private extension DateFormatter { | |
static let debugLogTimeFormatter: DateFormatter = { | |
let timeFormatter = DateFormatter() | |
timeFormatter.dateFormat = "HH:mm:ss.SSS" | |
return timeFormatter | |
}() | |
#if TEST | |
static let isoDateFormatter: DateFormatter = { | |
let timeFormatter = DateFormatter() | |
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" | |
return timeFormatter | |
}() | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment