Log improvements

This commit is contained in:
Michal Srutek 2022-08-14 14:35:16 +02:00
parent 9d9ac85c62
commit f0d658d0e3

View file

@ -1,14 +1,14 @@
import Foundation import Foundation
struct Log { struct Log {
static var dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSSSZ" private static let dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSSSZ"
static var dateFormatter: DateFormatter { private static let dateFormatter: DateFormatter = {
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.dateFormat = dateFormat formatter.dateFormat = dateFormat
formatter.locale = Locale.current formatter.locale = .current
formatter.timeZone = TimeZone.current formatter.timeZone = .current
return formatter return formatter
} }()
static func d(_ tag: String, _ message: String, _ other: Any?...) { static func d(_ tag: String, _ message: String, _ other: Any?...) {
log(.debug, tag, message, other) log(.debug, tag, message, other)
@ -26,22 +26,22 @@ struct Log {
log(.error, tag, message, other) log(.error, tag, message, other)
} }
static func log(_ level: LogLevel, _ tag: String, _ message: String, _ other: Any?...) { private static func log(_ level: LogLevel, _ tag: String, _ message: String, _ other: Any?...) {
print("\(dateStr()) ntfyApp [\(levelStr(level))] \(tag): \(message)") print("\(dateStr()) ntfyApp [\(levelStr(level))] \(tag): \(message)")
if !other.isEmpty { if !other.isEmpty {
other.forEach { o in other.forEach { o in
if o != nil { if let o = o {
print(" ", o!) print(" ", o)
} }
} }
} }
} }
static func dateStr() -> String { private static func dateStr() -> String {
return dateFormatter.string(from: Date()) dateFormatter.string(from: Date())
} }
static func levelStr(_ level: LogLevel) -> String { private static func levelStr(_ level: LogLevel) -> String {
switch level { switch level {
case .debug: return "DEBUG" case .debug: return "DEBUG"
case .info: return "INFO" case .info: return "INFO"
@ -51,7 +51,7 @@ struct Log {
} }
} }
enum LogLevel { private enum LogLevel {
case debug case debug
case info case info
case warning case warning