Logging
On this page
You can set or change your app's log level when developing or debugging your app. You might want to change the log level to log different amounts of data depending on your development needs. You can specify different log levels or custom loggers.
Set or Change the Log Level
You can set the level of detail reported by Atlas Device SDK.
For the C++ SDK, pass a
realm::logger::level
to the set_default_level_threshold()
member function:
auto logLevel = realm::logger::level::info; realm::set_default_level_threshold(logLevel);
You can change the log level to increase or decrease verbosity at different points in your code.
// The documentation does not currently have this code example in C++. // Please refer to the other languages or related pages for example code.
Customize the Logger
You can create a custom logger. You might want to customize logging to add specific tags or set specific log levels during development, testing, or debugging.
To set a custom logger function with the C++ SDK, create a
realm::logger
and override the virtual do_log()
member function.
struct MyCustomLogger : realm::logger { // This could be called from any thread, so may not output visibly to the // console. Handle output in a queue or other cross-thread context if needed. void do_log(realm::logger::level level, const std::string &msg) override { std::cout << "Realm log entry: " << msg << std::endl; } };
After setting a custom logger, you need to initialize it:
auto config = realm::db_config(); auto thisRealm = realm::db(config); auto myLogger = std::make_shared<MyCustomLogger>(); realm::set_default_logger(myLogger);
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.
Turn Off Logging
You can turn off logging by setting the log level:
// The documentation does not currently have this code example in C++. // Please refer to the other languages or related pages for example code.
Other Loggers
The C++ and Java SDKs have additional loggers that behave differently than the other examples on this page.
C++ SDK
The C++ SDK uses a sync logger that behaves differently than the rest of the examples on this page. You can set or change your sync client's log level to develop or debug your application. You might want to change the log level to log different amounts of data depending on the app's environment.
You can set the level of detail reported by the sync client logger to
specify the level of output you want from the sync client. Get
an instance of an App's sync manager,
and pass a realm::logger::level
to the set_log_level()
member function:
auto logLevel = realm::logger::level::info; app.get_sync_manager().set_log_level(logLevel);
Java SDK
The SDK logs events to the Android system log automatically. You can view these events using Logcat.
We recommend using the Kotlin SDK to get the latest logging updates, like changing the log level at different points in your code.
The Java SDK uses the log levels defined by Log4J. To configure the log level for database logs in your application, pass a LogLevel to RealmLog.setLevel():
RealmLog.setLevel(LogLevel.ALL);