Docs Menu
Docs Home
/ /
Atlas Device SDKs
/

Logging - Swift SDK

On this page

  • Set or Change the Realm Log Level
  • Set the Logger Before Initializing an App Client
  • Turn Off Logging
  • Customize the Logging Function

You can set or change your app's log level when developing or debugging your application. You might want to change the log level to log different amounts of data depending on your development needs.

Tip

See also:

This page shows how to set a Realm logger, which was added in Realm Swift SDK v10.39.0. This supersedes setting the Sync client log level in earlier versions of the Realm Swift SDK. For information on how to set the Sync client log level in an earlier version, refer to Set the Client Log Level - Swift SDK.

You can set the level of detail reported by the Realm Swift SDK. Set the log level for the default logger with Logger.shared.level:

Logger.shared.level = .trace

The RLMLogLevel enum represents the different levels of logging you can configure.

You can change the log level to increase or decrease verbosity at different points in your code. This behavior differs from the deprecated sync client log level, which had to be set before opening a synced realm and could not be changed.

// Set a log level that isn't too verbose
Logger.shared.level = .warn
// Later, when trying to debug something, change the log level for more verbosity
Logger.shared.level = .trace

When you initialize an App client, the Swift SDK caches the configuration for the App. Changing to the App configuration after initialization does not have any effect. This includes setting a logger. Initializing an App reads the current shared logger and stores that.

However, changing the log level for an existing logger does work at any time.

The default log threshold level for the Realm Swift SDK is .info. This displays some information in the console. You can disable logging entirely by setting the log level to .off:

Logger.shared.level = .off

Initialize an instance of a Logger and define the function to use for logging.

// Create an instance of `Logger` and define the log function to invoke.
let logger = Logger(level: .detail) { level, message in
// You may pass log information to a logging service, or
// you could simply print the logs for debugging. Define
// the log function that makes sense for your application.
print("REALM DEBUG: \(Date.now) \(level) \(message) \n")
}

Tip

To diagnose and troubleshoot errors while developing your application, set the log level to debug or trace. For production deployments, decrease the log level for improved performance.

You can set a logger as the default logger for your app with Logger.shared. After you set the default logger, you can change the log level during the app lifecycle as needed.

let logger = Logger(level: .info) { level, message in
// You may pass log information to a logging service, or
// you could simply print the logs for debugging. Define
// the log function that makes sense for your application.
print("REALM DEBUG: \(Date.now) \(level) \(message) \n")
}
// Set a logger as the default
Logger.shared = logger
// After setting a default logger, you can change
// the log level at any point during the app lifecycle
Logger.shared.level = .debug
← Test and Debug - Swift SDK