33 * SPDX-License-Identifier: Apache-2.0.
44 */
55
6- import AwsCommonRuntimeKit
7-
86public 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+
3441public 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}
0 commit comments