Shutdown

Send buffered logs before the page or process goes away.

In the browser

You don’t need to do anything. When the page is hidden or unloaded, the SDK sends what is waiting in a request the browser completes in the background. See When the visitor leaves.

flush()

await logger.flush() sends everything waiting now and resolves when the request has finished, retries included. The logger keeps running.

shutdown()

await logger.shutdown() stops the logger:

  • stops the flush timer and session replay;
  • removes every browser hook and restores the originals;
  • sends everything still waiting.

Call logger.init() afterwards to start it again. Calling init() on a logger that is already running only prints a warning.

In Node.js

When it starts in Node.js, the SDK adds its own handlers for SIGINT and SIGTERM. Each one calls shutdown() and then exits the process with code 0.

The SDK ends the process on SIGTERM

Because of those handlers, the process exits as soon as the SDK has sent its logs, even if your own SIGTERM handler is still closing connections or finishing requests. If your server needs a graceful shutdown, test it with the SDK in place.

For a script that should end by itself, flush before it finishes; the flush timer otherwise keeps the process alive:

TypeScript
await runJob();
await logger.shutdown(); // sends the last logs, stops the timer, lets Node exit

Serverless functions

A serverless platform may freeze or stop the function as soon as it returns, before a batch goes out. Await logger.flush() before returning the response. The Next.js guide shows it in a route handler.