Docs Menu
Docs Home
/ /
Atlas Device SDKs
/

Logging - Node.js SDK

On this page

  • Set or Change the Realm Log Level
  • Customize the Logger
  • Turn Off Logging
  • Performance and console.log()

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 Node.js SDK v12.0.0. This supersedes setting the Sync client log level in earlier versions of the Realm Node.js SDK. For information on how to set the Sync client log level in an earlier version, refer to Set the Client Log Level - Node.js SDK.

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

  • "all"

  • "trace"

  • "debug"

  • "detail"

  • "info"

  • "warn"

  • "error"

  • "fatal"

  • "off"

Realm.setLogLevel("all");

You can set different log levels 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 default log level that's not too verbose
Realm.setLogLevel("detail");
const realm = await Realm.open({
schema: [Turtle],
});
// Later, change the log level to debug an issue when running specific code
Realm.setLogLevel("trace");
realm.write(() => realm.create(Turtle, newTestObject()));

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

let logs = [];
Realm.setLogger((level, message) => {
logs.push({ level, message });
});
type Log = {
message: string;
level: string;
};
let logs: Log[] = [];
Realm.setLogger((level, message) => {
logs.push({ level, message });
});

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

You can turn off logging by setting the log level to RealmLogLevel.off:

Realm.setLogLevel("off");

You should avoid using console.log() in production because it will negatively affect your app's performance. It can also be hard to account for all of the method's quirks in Node.js.

For details about console.log() behavior, check out the Node.js docs.

Back

Partition-Based Sync

Next

SDK Telemetry