Docs Menu
Docs Home
/ /
Atlas Device SDKs
/

Logging - React Native SDK

On this page

  • Set or Change the Realm Log Level
  • Customize the Logger

You can set or change your app'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 specify different log levels or custom loggers.

Tip

See also:

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

You can set the level of detail reported by the Realm React Native SDK. To configure the log level, pass a valid level string value to setLogLevel():

  • "all"

  • "trace"

  • "debug"

  • "detail"

  • "info"

  • "warn"

  • "error"

  • "fatal"

  • "off"

Realm.setLogLevel('trace');

Use setLogLevel() anywhere in your app to increase or decrease log 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.

To turn off logging, pass "off" to setLogLevel():

Realm.setLogLevel('off');

To set a custom logger, call setLogger(). This method recieves level and message arguments from the Realm logger, not individual realms. Use these arguments to define your own logging behavior.

You must use setLogger() before you open a realm with RealmProvider. You can't use setLogger() in a hook in the same component as RealmProvider because RealmProvider opens a realm when it is mounted. Hooks generally run after a component is mounted, which means RealmProvider already opened a realm.

Most of the time, you should set your custom logger outside of the React tree. For example, in your app's root index.js file.

Realm.setLogger((level, message) => {
const log = {
message,
level,
};
setLogs(previousLogs => [...previousLogs, log]);
});

This sets the logging behavior for all Realm logging in your application, regardless of where you set it. If you do not provide a log level, the default is "info".

← Troubleshooting - React Native SDK