@@ -63,11 +63,29 @@ public final class NKLogFileManager: @unchecked Sendable {
6363 /// Configures the shared logger instance.
6464 /// - Parameters:
6565 /// - minLevel: The minimum log level to be recorded.
66-
6766 public static func configure( logLevel: NKLogLevel = . normal) {
6867 shared. setConfiguration ( logLevel: logLevel)
6968 }
7069
70+ /// Creates the "Logs" folder inside the user's Documents directory if it does not already exist.
71+ ///
72+ /// This static method delegates to the singleton instance (`shared`) and ensures
73+ /// that the log folder structure is created or re-created when needed.
74+ ///
75+ /// This is useful in scenarios where the log folder may have been deleted externally
76+ /// (e.g., by iTunes File Sharing, iCloud Drive sync conflicts, or cleanup tools),
77+ /// and must be re-initialized manually.
78+ ///
79+ /// The folder path is:
80+ /// `~/Documents/Logs`
81+ ///
82+ /// If the folder already exists, the method does nothing. If creation fails, the error is silently ignored.
83+ ///
84+ /// - Note: This does not create or write any log file, only the folder itself.
85+ public static func createLogsFolder( ) {
86+ shared. createLogsFolder ( )
87+ }
88+
7189 /// Returns the file URL of the currently active log file.
7290 public func currentLogFileURL( ) -> URL {
7391 return logDirectory. appendingPathComponent ( logFileName)
@@ -101,6 +119,28 @@ public final class NKLogFileManager: @unchecked Sendable {
101119 self . currentLogDate = Self . currentDateString ( )
102120 }
103121
122+ /// Creates the "Logs" folder inside the user's Documents directory if it does not already exist.
123+ ///
124+ /// This method performs the following steps:
125+ /// - Retrieves the path to the `.documentDirectory` using `FileManager`.
126+ /// - Appends a "Logs" subdirectory path.
127+ /// - Checks if the folder already exists.
128+ /// - If not, it creates the folder, including any intermediate directories.
129+ /// - Finally, it sets the `logDirectory` and initializes the current log date.
130+ ///
131+ /// If folder creation fails, the method silently ignores the error.
132+ ///
133+ /// - Note: The `logDirectory` property will point to the created `Logs` folder.
134+ ///
135+ private func createLogsFolder( ) {
136+ let documents = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . first!
137+ let logsFolder = documents. appendingPathComponent ( " Logs " , isDirectory: true )
138+ if !FileManager. default. fileExists ( atPath: logsFolder. path) {
139+ try ? FileManager . default. createDirectory ( at: logsFolder, withIntermediateDirectories: true )
140+ }
141+ self . currentLogDate = Self . currentDateString ( )
142+ }
143+
104144 /// Sets configuration parameters for the logger.
105145 /// - Parameters:
106146 /// - logLevel: The NKLogLevel { disabled .. verbose }
@@ -243,6 +283,11 @@ public final class NKLogFileManager: @unchecked Sendable {
243283 private func appendToLog( _ message: String ) {
244284 let logPath = logDirectory. appendingPathComponent ( logFileName)
245285
286+ // Ensure log directory exists
287+ if !fileManager. fileExists ( atPath: logDirectory. path) {
288+ try ? fileManager. createDirectory ( at: logDirectory, withIntermediateDirectories: true )
289+ }
290+
246291 guard let data = message. data ( using: . utf8) else { return }
247292
248293 if fileManager. fileExists ( atPath: logPath. path) {
0 commit comments