@@ -25,6 +25,7 @@ public struct FileLogHandler: LogHandler {
2525 public var metadata : Logger . Metadata = [ : ]
2626
2727 private let label : String
28+ private let category : String
2829 private let fileHandle : FileHandle
2930
3031 public subscript( metadataKey metadataKey: String ) -> Logger . Metadata . Value ? {
@@ -36,8 +37,17 @@ public struct FileLogHandler: LogHandler {
3637 }
3738 }
3839
39- public init ( label: String , path: FilePath ) throws {
40+ /// Create a log handler that appends to the specified file.
41+ ///
42+ /// - Parameters:
43+ /// - label: A unique identifier for the application.
44+ /// - category: An identifier for the application subsystem.
45+ /// - path: The log file location. The log handler creates the
46+ /// file and parent directory if needed.
47+ /// - Returns: The log handler.
48+ public init ( label: String , category: String , path: FilePath ) throws {
4049 self . label = label
50+ self . category = category
4151 let parentPath = path. removingLastComponent ( )
4252 try FileManager . default. createDirectory ( atPath: parentPath. string, withIntermediateDirectories: true )
4353 if !FileManager. default. fileExists ( atPath: path. string) {
@@ -74,16 +84,18 @@ public struct FileLogHandler: LogHandler {
7484
7585 let text : String
7686 if !effectiveMetadata. isEmpty {
77- text = " \( timestamp) [ \( level) ] \( label) \( effectiveMetadata. description) : \( message) \n "
87+ text = " \( timestamp) [ \( level) ] \( label) \( category ) \( effectiveMetadata. description) : \( message) \n "
7888 } else {
79- text = " \( timestamp) [ \( level) ] \( label) : \( message) \n "
89+ text = " \( timestamp) [ \( level) ] \( label) : \( category ) \( message) \n "
8090 }
8191 if let data = text. data ( using: . utf8) {
8292 fileHandle. write ( data)
8393 }
8494 }
8595
96+ /// Failures relating to the log handler.
8697 public enum FileLogFailure : Error {
98+ /// The log handler could not open the log file.
8799 case openFailed
88100 }
89101}
0 commit comments