mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Merge branch 'feature/log_levels' into develop
This commit is contained in:
commit
ed25071b18
3 changed files with 43 additions and 17 deletions
|
@ -58,22 +58,48 @@ public protocol Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Log level controls the conditions under which a message should be logged.
|
/// Log level controls the conditions under which a message should be logged.
|
||||||
public enum LogLevel: String {
|
/// - note: [RFC5424](https://www.rfc-editor.org/info/rfc5424)
|
||||||
|
public enum LogLevel: Int {
|
||||||
|
|
||||||
/// Use this level to capture information about things that might result a failure.
|
/// System is unusable.
|
||||||
case `default` = "Default"
|
case emergency = 0
|
||||||
|
|
||||||
/// Use this level to capture information that may be helpful, but isn’t essential, for troubleshooting errors.
|
/// Action must be taken immediately.
|
||||||
case info = "Info"
|
case alert = 1
|
||||||
|
|
||||||
/// Use this level to capture information that may be useful during development or while troubleshooting a specific problem.
|
/// Critical conditions.
|
||||||
case debug = "Debug"
|
case critical = 2
|
||||||
|
|
||||||
/// Use this log level to capture process-level information to report errors in the process.
|
/// Error conditions.
|
||||||
case error = "Error"
|
case error = 3
|
||||||
|
|
||||||
/// Use this level to capture system-level or multi-process information to report system errors.
|
/// Warning conditions.
|
||||||
case fault = "Fault"
|
case warning = 4
|
||||||
|
|
||||||
|
/// Normal but significant conditions.
|
||||||
|
case notice = 5
|
||||||
|
|
||||||
|
/// Informational messages.
|
||||||
|
case informational = 6
|
||||||
|
|
||||||
|
/// Debug-level messages.
|
||||||
|
case debug = 7
|
||||||
|
}
|
||||||
|
|
||||||
|
extension LogLevel: LogStringConvertible {
|
||||||
|
|
||||||
|
public var logDescription: String {
|
||||||
|
switch self {
|
||||||
|
case .emergency: return "emerg"
|
||||||
|
case .alert: return "alert"
|
||||||
|
case .critical: return "crit"
|
||||||
|
case .error: return "err"
|
||||||
|
case .warning: return "warning"
|
||||||
|
case .notice: return "notice"
|
||||||
|
case .informational: return "info"
|
||||||
|
case .debug: return "debug"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Logger {
|
extension Logger {
|
||||||
|
@ -82,11 +108,11 @@ extension Logger {
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - object: The object to be logged.
|
/// - object: The object to be logged.
|
||||||
/// - level: The log level. If unspecified, the `default` log level is used.
|
/// - level: The log level.
|
||||||
/// - file: **Do not provide a custom value.** The path to the file log method is called from.
|
/// - file: **Do not provide a custom value.** The path to the file log method is called from.
|
||||||
/// - line: **Do not provide a custom value.** The line number log method is called at.
|
/// - line: **Do not provide a custom value.** The line number log method is called at.
|
||||||
/// - function: **Do not provide a custom value.** The name of the declaration log method is called within.
|
/// - function: **Do not provide a custom value.** The name of the declaration log method is called within.
|
||||||
public func log(_ object: Any, level: LogLevel = .default, file: String = #file, line: Int = #line, function: String = #function) {
|
public func log(_ object: Any, level: LogLevel, file: String = #file, line: Int = #line, function: String = #function) {
|
||||||
let now = Date()
|
let now = Date()
|
||||||
let location = description(for: file, line: line, function: function)
|
let location = description(for: file, line: line, function: function)
|
||||||
let objectDescription = description(for: object)
|
let objectDescription = description(for: object)
|
||||||
|
|
|
@ -36,6 +36,6 @@ public final class ConsoleLogger: Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
||||||
print(formatter.string(from: time) + " <" + level.rawValue + "> " + location + " " + object)
|
print(formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,13 +51,13 @@ public final class DiskLogger: Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
||||||
let message = formatter.string(from: time) + " <" + level.rawValue + "> " + location + " " + object + "\n"
|
let message = formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + object + "\n"
|
||||||
log(message)
|
log(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func log(_ message: String) {
|
private func log(_ message: String) {
|
||||||
guard let data = message.data(using: .utf8) else {
|
guard let data = message.data(using: .utf8) else {
|
||||||
log("Message failed to convert to UTF8")
|
log("Message failed to convert to UTF8", level: .warning)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
queue.async {
|
queue.async {
|
||||||
|
@ -73,7 +73,7 @@ public final class DiskLogger: Logger {
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
let message = String(describing: error)
|
let message = String(describing: error)
|
||||||
self.log(message)
|
self.log(message, level: .warning)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue