Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,19 @@ extension Logger.Level: Comparable {
}
}

extension Logger.Level: CustomStringConvertible, LosslessStringConvertible {
/// A textual representation of the log level.
public var description: String {
self.rawValue
}

/// Creates a log level from its textual representation.
/// - Parameter description: A textual representation of the log level, case insensitive.
public init?(_ description: String) {
self.init(rawValue: description.lowercased())
}
}

// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
// https://bugs.swift.org/browse/SR-9687
// Then we could write it as follows and it would work under Swift 5 and not only 4 as it does currently:
Expand Down
5 changes: 5 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,11 @@ struct LoggingTest {
#expect(Logger.Level.error < Logger.Level.critical)
}

@Test(arguments: Logger.Level.allCases) func logLevelDescription(level: Logger.Level) {
#expect(level.description == level.rawValue)
#expect(Logger.Level(level.rawValue.uppercased()) == level)
}

final class InterceptStream: TextOutputStream {
var interceptedText: String?
var strings = [String]()
Expand Down
Loading