@@ -60,11 +60,13 @@ extension Logger {
60
60
public func log( level: Logger . Level ,
61
61
_ message: @autoclosure ( ) -> Logger . Message ,
62
62
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
63
- file: String = #file, function: String = #function, line: UInt = #line) {
63
+ source: @autoclosure ( ) -> String ? = nil ,
64
+ file: String = ( #file) , function: String = #function, line: UInt = #line) {
64
65
if self . logLevel <= level {
65
66
self . handler. log ( level: level,
66
67
message: message ( ) ,
67
68
metadata: metadata ( ) ,
69
+ source: source ( ) ?? Logger . currentModule ( filePath: file) ,
68
70
file: file, function: function, line: line)
69
71
}
70
72
}
@@ -118,8 +120,9 @@ extension Logger {
118
120
@inlinable
119
121
public func trace( _ message: @autoclosure ( ) -> Logger . Message ,
120
122
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
123
+ source: @autoclosure ( ) -> String ? = nil ,
121
124
file: String = #file, function: String = #function, line: UInt = #line) {
122
- self . log ( level: . trace, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
125
+ self . log ( level: . trace, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
123
126
}
124
127
125
128
/// Log a message passing with the `Logger.Level.debug` log level.
@@ -139,8 +142,9 @@ extension Logger {
139
142
@inlinable
140
143
public func debug( _ message: @autoclosure ( ) -> Logger . Message ,
141
144
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
145
+ source: @autoclosure ( ) -> String ? = nil ,
142
146
file: String = #file, function: String = #function, line: UInt = #line) {
143
- self . log ( level: . debug, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
147
+ self . log ( level: . debug, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
144
148
}
145
149
146
150
/// Log a message passing with the `Logger.Level.info` log level.
@@ -160,8 +164,9 @@ extension Logger {
160
164
@inlinable
161
165
public func info( _ message: @autoclosure ( ) -> Logger . Message ,
162
166
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
167
+ source: @autoclosure ( ) -> String ? = nil ,
163
168
file: String = #file, function: String = #function, line: UInt = #line) {
164
- self . log ( level: . info, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
169
+ self . log ( level: . info, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
165
170
}
166
171
167
172
/// Log a message passing with the `Logger.Level.notice` log level.
@@ -181,8 +186,9 @@ extension Logger {
181
186
@inlinable
182
187
public func notice( _ message: @autoclosure ( ) -> Logger . Message ,
183
188
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
189
+ source: @autoclosure ( ) -> String ? = nil ,
184
190
file: String = #file, function: String = #function, line: UInt = #line) {
185
- self . log ( level: . notice, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
191
+ self . log ( level: . notice, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
186
192
}
187
193
188
194
/// Log a message passing with the `Logger.Level.warning` log level.
@@ -202,8 +208,9 @@ extension Logger {
202
208
@inlinable
203
209
public func warning( _ message: @autoclosure ( ) -> Logger . Message ,
204
210
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
211
+ source: @autoclosure ( ) -> String ? = nil ,
205
212
file: String = #file, function: String = #function, line: UInt = #line) {
206
- self . log ( level: . warning, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
213
+ self . log ( level: . warning, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
207
214
}
208
215
209
216
/// Log a message passing with the `Logger.Level.error` log level.
@@ -223,8 +230,9 @@ extension Logger {
223
230
@inlinable
224
231
public func error( _ message: @autoclosure ( ) -> Logger . Message ,
225
232
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
233
+ source: @autoclosure ( ) -> String ? = nil ,
226
234
file: String = #file, function: String = #function, line: UInt = #line) {
227
- self . log ( level: . error, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
235
+ self . log ( level: . error, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
228
236
}
229
237
230
238
/// Log a message passing with the `Logger.Level.critical` log level.
@@ -243,8 +251,9 @@ extension Logger {
243
251
@inlinable
244
252
public func critical( _ message: @autoclosure ( ) -> Logger . Message ,
245
253
metadata: @autoclosure ( ) -> Logger . Metadata ? = nil ,
254
+ source: @autoclosure ( ) -> String ? = nil ,
246
255
file: String = #file, function: String = #function, line: UInt = #line) {
247
- self . log ( level: . critical, message ( ) , metadata: metadata ( ) , file: file, function: function, line: line)
256
+ self . log ( level: . critical, message ( ) , metadata: metadata ( ) , source : source ( ) , file: file, function: function, line: line)
248
257
}
249
258
}
250
259
@@ -488,9 +497,18 @@ public struct MultiplexLogHandler: LogHandler {
488
497
public func log( level: Logger . Level ,
489
498
message: Logger . Message ,
490
499
metadata: Logger . Metadata ? ,
491
- file: String , function: String , line: UInt ) {
500
+ source: String ,
501
+ file: String ,
502
+ function: String ,
503
+ line: UInt ) {
492
504
self . handlers. forEach { handler in
493
- handler. log ( level: level, message: message, metadata: metadata, file: file, function: function, line: line)
505
+ handler. log ( level: level,
506
+ message: message,
507
+ metadata: metadata,
508
+ source: source,
509
+ file: file,
510
+ function: function,
511
+ line: line)
494
512
}
495
513
}
496
514
@@ -618,7 +636,10 @@ public struct StreamLogHandler: LogHandler {
618
636
public func log( level: Logger . Level ,
619
637
message: Logger . Message ,
620
638
metadata: Logger . Metadata ? ,
621
- file: String , function: String , line: UInt ) {
639
+ source: String ,
640
+ file: String ,
641
+ function: String ,
642
+ line: UInt ) {
622
643
let prettyMetadata = metadata? . isEmpty ?? true
623
644
? self . prettyMetadata
624
645
: self . prettify ( self . metadata. merging ( metadata!, uniquingKeysWith: { _, new in new } ) )
@@ -644,6 +665,20 @@ public struct StreamLogHandler: LogHandler {
644
665
}
645
666
}
646
667
668
+ extension Logger {
669
+ @inlinable
670
+ internal static func currentModule( filePath: String = #filePath) -> String {
671
+ let utf8All = filePath. utf8
672
+ return filePath. utf8. lastIndex ( of: UInt8 ( ascii: " / " ) ) . flatMap { lastSlash -> Substring ? in
673
+ utf8All [ ..< lastSlash] . lastIndex ( of: UInt8 ( ascii: " / " ) ) . map { secondLastSlash -> Substring in
674
+ filePath [ utf8All. index ( after: secondLastSlash) ..< lastSlash]
675
+ }
676
+ } . map {
677
+ String ( $0)
678
+ } ?? " n/a "
679
+ }
680
+ }
681
+
647
682
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
648
683
// https://bugs.swift.org/browse/SR-9686
649
684
extension Logger . MetadataValue : ExpressibleByStringLiteral {
0 commit comments