DnsUpdater/Sources/App/Logger.swift
Christoph Hagen 9b6fd627ac First version
2024-11-15 10:46:29 +01:00

53 lines
1.5 KiB
Swift

import Foundation
final class Log: @unchecked Sendable {
enum Level: String, Codable {
case debug
case info
case warning
case error
var level: Int {
switch self {
case .debug: return 0
case .info: return 1
case .warning: return 2
case .error: return 3
}
}
}
private var logLevel: Level = .warning
private let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
return formatter
}()
init(logLevel: Level) {
self.logLevel = logLevel
}
private func log(_ level: Level, message: String) {
guard level.rawValue >= logLevel.rawValue else {
return
}
print("[\(formatter.string(from: Date()))][\(level.rawValue.uppercased())] \(message)")
}
func debug(_ message: String) { log(.debug, message: message) }
func info(_ message: String) { log(.info, message: message) }
func warning(_ message: String) { log(.warning, message: message) }
func error(_ message: String) { log(.error, message: message) }
static func debug(_ message: String) { log.debug(message) }
static func info(_ message: String) { log.info(message) }
static func warning(_ message: String) { log.warning(message) }
static func error(_ message: String) { log.error(message) }
static let log = Log(logLevel: .warning)
}