Configuration
All Logly settings live in one shared object: LoggerConfiguration.shared. They apply to every logger in the app and can be changed at runtime at any time.
All LoggerConfiguration settings
The following table lists all properties of LoggerConfiguration in Logly with type, default value and effect:
| Property | Type | Default | Effect |
|---|---|---|---|
currentLogLevel | LogLevel | .debug | Minimum level; anything below is dropped |
logToFile | Bool | true | Also write messages to a file |
logFilePath | URL | app_log.txt in the temporary directory | Path of the active log file |
logFormat | String | "{timestamp} - {level} - {category} - {file}:{line} - {message}" | Layout of each line, see Output format |
logLevelWidth | Int | 7 | Fixed width of the {level} placeholder |
categoryWidth | Int | 12 | Fixed width of the {category} placeholder |
enableANSIColors | Bool | true | Color console output by level |
asynchronousLogging | Bool | true | Process on a background queue |
maxFileSizeInBytes | Int64 | 5242880 (5 MB) | Rotate at this file size |
maxLogAge | TimeInterval | 86400 (24 hours) | Rotate at this file age, in seconds |
maxRotatedFiles | Int | 5 | Number of rotated files to keep |
sentryURL | String? | nil | Sentry DSN for LoglySentry |
sinks | [LogSink] | [] | Registered sinks (read-only) |
LoggerConfiguration has two methods for sinks:
addSink(_:)registers a sink, see Custom sinks.removeAllSinks()removes all sinks, including one registered by LoglySentry.
Synchronous LoggerConfiguration API
In the synchronous Logly API you set and read the properties of LoggerConfiguration directly:
let config = LoggerConfiguration.shared
config.currentLogLevel = .info
config.logToFile = true
config.enableANSIColors = false
let level = config.currentLogLevel
Asynchronous LoggerConfiguration API
In the asynchronous Logly API every property except sinks has a set…/get… pair:
let config = LoggerConfiguration.shared
await config.setCurrentLogLevel(.warning)
await config.setLogToFile(true)
await config.setLogFilePath(logURL)
await config.setLogFormat("{level} {message}")
await config.setLogLevelWidth(7)
await config.setCategoryWidth(12)
await config.setEnableANSIColors(false)
await config.setAsynchronousLogging(true)
await config.setMaxFileSizeInBytes(10 * 1024 * 1024)
await config.setMaxLogAge(60 * 60 * 24 * 7)
await config.setMaxRotatedFiles(10)
await config.setSentryURL(nil)
let level = await config.getCurrentLogLevel()
let path = await config.getLogFilePath()
The async methods call the synchronous properties internally. They behave the same and are meant for code that already runs in an async context.
Thread safety of the Logly configuration
The whole state of the Logly configuration lives in one struct protected by an OSAllocatedUnfairLock. Reading and writing is allowed from any thread.
At the start of every log call Logly reads a snapshot of level, format, widths, colors, logToFile, asynchronousLogging and sinks. A change therefore takes effect with the next log call. The file path and the rotation settings are only read when the line is actually written to the file.
Asynchronous logging in Logly
With asynchronousLogging = true (the default) the Logly call only checks the level and hands the remaining work to a serial queue with QoS .utility. Formatting, console output, file access and sinks run there. The calling thread is not blocked by file I/O, and the order of messages is preserved.
With asynchronousLogging = false everything runs immediately on the calling thread. The call only returns once the line has been written and all sinks have been called.
The following table recommends a value for asynchronousLogging for each situation:
| Situation | Recommendation |
|---|---|
| App with UI, many messages | true |
| Command-line tool that exits right after its last message | false, otherwise queued messages can be lost |
| Unit tests that check the log file | false |
Crash log from LoggerCrashHandler must end up in the file | false, see Crash handling |
Logly configuration examples
Logly configuration for development
This function configures Logly for development with all messages and immediate output:
func configureDevelopmentLogging() {
let config = LoggerConfiguration.shared
config.currentLogLevel = .debug
config.logFormat = "{timestamp} {level} [{category}] {file}:{line} {message}"
config.enableANSIColors = true // in a terminal; better false in the Xcode console
config.asynchronousLogging = false // immediate output, easier when debugging
config.maxFileSizeInBytes = 1 * 1024 * 1024
config.maxRotatedFiles = 3
}
Logly configuration for production
This function configures Logly for production with its own log folder, minimum level .info and rotation:
func configureProductionLogging() throws {
let config = LoggerConfiguration.shared
let logsDirectory = FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
.appendingPathComponent("Logs")
try FileManager.default.createDirectory(at: logsDirectory, withIntermediateDirectories: true)
config.currentLogLevel = .info
config.logToFile = true
config.logFilePath = logsDirectory.appendingPathComponent("app.log")
config.logFormat = "{timestamp} {level} {category} {message}"
config.enableANSIColors = false
config.asynchronousLogging = true
config.maxFileSizeInBytes = 10 * 1024 * 1024 // 10 MB
config.maxLogAge = 60 * 60 * 24 * 7 // 7 days
config.maxRotatedFiles = 5
}
Switching the Logly configuration by build configuration
This is how you choose the Logly configuration depending on the build configuration:
#if DEBUG
configureDevelopmentLogging()
#else
try? configureProductionLogging()
#endif
Logly to the console only, no file
This is how Logly writes only to the console and not to a file:
LoggerConfiguration.shared.logToFile = false
Changing the Logly level at runtime
You change the Logly log level at runtime, for example through a switch in the developer settings:
struct DeveloperSettingsView: View {
@State private var verbose = LoggerConfiguration.shared.currentLogLevel == .debug
var body: some View {
Toggle("Verbose logging", isOn: Binding(
get: { verbose },
set: { newValue in
verbose = newValue
LoggerConfiguration.shared.currentLogLevel = newValue ? .debug : .info
}
))
}
}
Logly configuration in an async context
In an async context you configure Logly with the set… and get… methods:
Task {
let config = LoggerConfiguration.shared
await config.setCurrentLogLevel(.info)
await config.setAsynchronousLogging(true)
let level = await config.getCurrentLogLevel()
await Logger.custom(category: "App").info("Log level is \(level.description)")
}