Log files
Logly can write every message as a formatted line to a file. The file is rotated automatically so that it does not grow without limit.
Turning file logging on and off in Logly
In Logly, file logging is on by default. Without further settings Logly writes to app_log.txt in the temporary directory (FileManager.default.temporaryDirectory). The system may clear that folder at any time, so set your own path for persistent logs. This is how you turn file logging on or off:
let config = LoggerConfiguration.shared
config.logToFile = true // default
config.logToFile = false // console and sinks only
Setting the location of the Logly log file
In Logly you set the location of the log file with logFilePath. The following example creates a Logs folder in Application Support for it:
let logsDirectory = FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
.appendingPathComponent("Logs")
try FileManager.default.createDirectory(at: logsDirectory, withIntermediateDirectories: true)
LoggerConfiguration.shared.logFilePath = logsDirectory.appendingPathComponent("app.log")
Logly creates the file but not missing folders. If the folder is missing, Logly prints [Logger] Failed to write log to file: … to the console for every message and writes nothing to the file.
Put the log file in a folder that contains only logs. During cleanup Logly deletes every file in the folder whose name matches the pattern of rotated files (see below).
How Logly writes to the log file
When writing to the log file, Logly works like this:
- Each message is appended as one line with a trailing line break, encoded as UTF-8.
- If the file does not exist yet, Logly creates it.
- Color codes are never written to the file.
- Before every write Logly checks whether the file needs to be rotated.
Rotation of the Logly log file
Logly rotates the active file as soon as one of these conditions is met. The following table lists both settings with their default and condition:
| Setting | Default | Condition |
|---|---|---|
maxFileSizeInBytes | 5 MB | File size is greater than or equal to the value |
maxLogAge | 24 hours | At least this much time has passed since the file's creation date |
The Logly rotation check only runs when a message is written. There is no timer. A file that receives no more messages stays as it is until the next message arrives.
Names of rotated Logly files
On rotation Logly renames the active file in the same folder according to this scheme:
<file name without extension>_<timestamp>.log
The timestamp is ISO 8601 in UTC with colons replaced by hyphens. The extension is always .log, even if the active file has a different one. The following table shows examples of active and rotated file names:
| Active file | Rotated file |
|---|---|
app.log | app_2026-10-03T14-10-52Z.log |
app_log.txt | app_log_2026-10-03T14-10-52Z.log |
The next message then creates a new, empty file under the original name.
Cleanup of rotated Logly files
Right after each rotation Logly cleans up in these steps:
- It looks for all files in the folder whose name starts with
<file name without extension>_and ends with.log. - It sorts them by creation date.
- It deletes the oldest ones until at most
maxRotatedFilesremain.
The active file does not count. With maxRotatedFiles = 0 every rotated file is deleted immediately.
Estimating the disk usage of Logly log files
The Logly log files take up at most maxRotatedFiles rotated files plus the active file. Because the size is checked before writing, a file can exceed the limit by one line. The following example shows a configuration with its upper bound:
let config = LoggerConfiguration.shared
config.maxFileSizeInBytes = 2 * 1024 * 1024 // 2 MB
config.maxLogAge = 60 * 60 * 24 // new file daily
config.maxRotatedFiles = 7 // keep one week
// Upper bound roughly 8 × 2 MB = 16 MB
Logly log file examples
Reading the current Logly log file
This function reads the contents of the active Logly log file:
func currentLogContents() -> String {
let url = LoggerConfiguration.shared.logFilePath
return (try? String(contentsOf: url, encoding: .utf8)) ?? ""
}
With asynchronousLogging = true a message that was just logged may still be in the queue and missing from the result.
Listing all Logly log files
This function lists the rotated Logly files and the active file:
func allLogFiles() -> [URL] {
let active = LoggerConfiguration.shared.logFilePath
let directory = active.deletingLastPathComponent()
let baseName = active.deletingPathExtension().lastPathComponent
let files = (try? FileManager.default.contentsOfDirectory(
at: directory,
includingPropertiesForKeys: [.creationDateKey]
)) ?? []
let rotated = files
.filter { $0.lastPathComponent.hasPrefix("\(baseName)_") && $0.pathExtension == "log" }
.sorted { $0.lastPathComponent < $1.lastPathComponent }
return rotated + [active]
}
Offering Logly logs for sharing (SwiftUI)
In SwiftUI you offer the active Logly log file for sharing with a ShareLink:
struct SupportView: View {
var body: some View {
ShareLink(item: LoggerConfiguration.shared.logFilePath) {
Label("Share log file", systemImage: "square.and.arrow.up")
}
}
}
Filtering errors from the Logly log file
This function filters error lines from the Logly log file. It works with the default format, in which the level appears as - ERROR or - FAULT:
func errorLines() -> [String] {
let text = (try? String(contentsOf: LoggerConfiguration.shared.logFilePath, encoding: .utf8)) ?? ""
return text
.split(separator: "\n")
.filter { $0.contains(" - ERROR ") || $0.contains(" - FAULT ") }
.map(String.init)
}
Deleting Logly logs
This function deletes all Logly log files:
func deleteAllLogs() {
for url in allLogFiles() {
try? FileManager.default.removeItem(at: url)
}
}
The next message creates the active file again.