Skip to content

Commit 567abc3

Browse files
authored
fix: logging interface to wrap swift-log (#396)
1 parent 6d90db5 commit 567abc3

File tree

2 files changed

+96
-86
lines changed

2 files changed

+96
-86
lines changed

Packages/ClientRuntime/Sources/Logging/LogAgent.swift

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
import AwsCommonRuntimeKit
7-
86
public protocol LogAgent {
97
/// name of the struct or class where the logger was instantiated from
108
var name: String {get}
119

1210
/// Get or set the configured log level.
13-
var level: LogLevel {get set}
11+
var level: LogAgentLevel {get set}
1412

1513
/// This method is called when a `LogAgent` must emit a log message.
1614
///
1715
/// - parameters:
18-
/// - level: The `LogLevel` the message was logged at.
16+
/// - level: The `LogAgentLevel` the message was logged at.
1917
/// - message: The message to log.
2018
/// - metadata: The metadata associated to this log message as a dictionary
2119
/// - source: The source where the log message originated, for example the logging module.
2220
/// - file: The file the log message was emitted from.
2321
/// - function: The function the log line was emitted from.
2422
/// - line: The line the log message was emitted from.
25-
func log(level: LogLevel,
23+
func log(level: LogAgentLevel,
2624
message: String,
2725
metadata: [String: String]?,
2826
source: String,
@@ -31,8 +29,16 @@ public protocol LogAgent {
3129
line: UInt)
3230
}
3331

32+
public enum LogAgentLevel: String, Codable, CaseIterable {
33+
case trace
34+
case debug
35+
case info
36+
case warn
37+
case error
38+
case fatal
39+
}
40+
3441
public extension LogAgent {
35-
3642
internal static func currentModule(filePath: String = #file) -> String {
3743
let utf8All = filePath.utf8
3844
return filePath.utf8.lastIndex(of: UInt8(ascii: "/")).flatMap { lastSlash -> Substring? in
@@ -44,69 +50,69 @@ public extension LogAgent {
4450
} ?? "n/a"
4551
}
4652

47-
/// Log a message passing with the `LogLevel.info` log level.
48-
func info(_ message: String) {
49-
self.log(level: LogLevel.info,
53+
/// Log a message passing with the `.info` log level.
54+
func info(_ message: String, file: String = #file, function: String = #function, line: UInt = #line) {
55+
self.log(level: .info,
5056
message: message,
5157
metadata: nil,
5258
source: Self.currentModule(),
53-
file: #file,
54-
function: #function,
55-
line: #line)
59+
file: file,
60+
function: function,
61+
line: line)
5662
}
5763

5864
/// Log a message passing with the `LogLevel.warn` log level.
59-
func warn(_ message: String) {
60-
self.log(level: LogLevel.warn,
65+
func warn(_ message: String, file: String = #file, function: String = #function, line: UInt = #line) {
66+
self.log(level: .warn,
6167
message: message,
6268
metadata: nil,
6369
source: Self.currentModule(),
64-
file: #file,
65-
function: #function,
66-
line: #line)
70+
file: file,
71+
function: function,
72+
line: line)
6773
}
6874

69-
/// Log a message passing with the `LogLevel.debug` log level.
70-
func debug(_ message: String) {
71-
self.log(level: LogLevel.debug,
75+
/// Log a message passing with the `.debug` log level.
76+
func debug(_ message: String, file: String = #file, function: String = #function, line: UInt = #line) {
77+
self.log(level: .debug,
7278
message: message,
7379
metadata: nil,
7480
source: Self.currentModule(),
75-
file: #file,
76-
function: #function,
77-
line: #line)
81+
file: file,
82+
function: function,
83+
line: line)
7884
}
7985

80-
/// Log a message passing with the `LogLevel.error` log level.
81-
func error(_ message: String) {
82-
self.log(level: LogLevel.error,
86+
/// Log a message passing with the `.error` log level.
87+
func error(_ message: String, file: String = #file, function: String = #function, line: UInt = #line) {
88+
self.log(level: .error,
8389
message: message,
8490
metadata: nil,
8591
source: Self.currentModule(),
86-
file: #file,
87-
function: #function,
88-
line: #line)
92+
file: file,
93+
function: function,
94+
line: line)
8995
}
9096

91-
/// Log a message passing with the `LogLevel.trace` log level.
92-
func trace(_ message: String) {
93-
self.log(level: LogLevel.trace,
97+
/// Log a message passing with the `.trace` log level.
98+
func trace(_ message: String, file: String = #file, function: String = #function, line: UInt = #line) {
99+
self.log(level: .trace,
94100
message: message,
95101
metadata: nil,
96102
source: Self.currentModule(),
97-
file: #file,
98-
function: #function,
99-
line: #line)
103+
file: file,
104+
function: function,
105+
line: line)
100106
}
101107

102-
/// Log a message passing with the `LogLevel.fatal` log level.
103-
func fatal(_ message: String) {
104-
self.log(level: LogLevel.fatal,
108+
/// Log a message passing with the `.fatal` log level.
109+
func fatal(_ message: String, file: String = #file, function: String = #function, line: UInt = #line) {
110+
self.log(level: .fatal,
105111
message: message,
106112
metadata: nil,
107113
source: Self.currentModule(),
108-
file: #file,
109-
function: #function,
110-
line: #line)
114+
file: file,
115+
function: function,
116+
line: line)
111117
}
112118
}

Packages/ClientRuntime/Sources/Logging/SwiftLog+LogAgent.swift

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,66 @@
44
*/
55

66
import Logging
7-
import enum AwsCommonRuntimeKit.LogLevel
87

9-
public typealias SwiftLogger = Logger
8+
public struct SwiftLogger: LogAgent {
9+
private let logger: Logger
10+
private let label: String
11+
private var logLevel: LogAgentLevel
1012

11-
extension SwiftLogger: LogAgent {
12-
13-
public var level: LogLevel {
13+
public init(label: String) {
14+
self.label = label
15+
self.logger = Logger(label: label)
16+
self.logLevel = LogAgentLevel.info
17+
}
18+
19+
public var level: LogAgentLevel {
1420
get {
15-
return LogLevel.fromString(string: logLevel.rawValue)
21+
return logLevel
1622
}
1723
set(value) {
18-
logLevel = Level.init(rawValue: value.stringValue) ?? Level.info
24+
logLevel = value
1925
}
2026
}
21-
27+
2228
public var name: String {
23-
return label
29+
return label
2430
}
2531

26-
public func log(level: LogLevel,
27-
message: String,
28-
metadata: [String: String]?,
29-
source: String,
30-
file: String,
31-
function: String,
32-
line: UInt) {
33-
let mappedDict = metadata?.mapValues { (value) -> MetadataValue in
34-
return MetadataValue.string(value)
32+
public func log(level: LogAgentLevel,
33+
message: String,
34+
metadata: [String: String]?,
35+
source: String,
36+
file: String,
37+
function: String,
38+
line: UInt) {
39+
let mappedDict = metadata?.mapValues { (value) -> Logger.MetadataValue in
40+
return Logger.MetadataValue.string(value)
3541
}
36-
self.log(level: Level.init(rawValue: level.stringValue) ?? Level.info,
37-
Message(stringLiteral: message),
38-
metadata: mappedDict,
39-
source: source)
42+
self.logger.log(level: logLevel.toLoggerLevel(),
43+
Logger.Message(stringLiteral: message),
44+
metadata: mappedDict,
45+
source: source,
46+
file: file,
47+
function: function,
48+
line: line)
4049
}
41-
42-
func info(_ message: String) {
43-
info(Message(stringLiteral: message))
44-
}
45-
46-
func debug(_ message: String) {
47-
debug(Message(stringLiteral: message))
48-
}
49-
50-
func warn(_ message: String) {
51-
warning(Message(stringLiteral: message))
52-
}
53-
54-
func error(_ message: String) {
55-
error(Message(stringLiteral: message))
56-
}
57-
58-
func trace(_ message: String) {
59-
trace(Message(stringLiteral: message))
60-
}
61-
62-
func fatal(_ message: String) {
63-
critical(Message(stringLiteral: message))
50+
}
51+
52+
extension LogAgentLevel {
53+
func toLoggerLevel() -> Logger.Level {
54+
switch self {
55+
case .trace:
56+
return .trace
57+
case .debug:
58+
return .debug
59+
case .info:
60+
return .info
61+
case .warn:
62+
return .warning
63+
case .error:
64+
return .error
65+
case .fatal:
66+
return .critical
67+
}
6468
}
6569
}

0 commit comments

Comments
 (0)