Docs Menu

Forward Logs to a Service

On this page

  • Overview
  • Why Should I Configure Log Forwarding?
  • How is Log Forwarding Billed?
  • Set Up a Log Forwarder
  • 1. Create a Log Forwarder
  • 2. Choose Which Logs to Forward
  • 3. Configure Log Batching
  • 5. Define an Action
  • 6. Save and Deploy your Changes
  • Restart a Suspended Log Forwarder

You can configure a log forwarder to automatically store your application's server-side logs in a MongoDB collection or send them to an external service. App Services can forward logs individually as they're created or batch logs together to reduce overhead.

A log forwarder consists of the following components:

  • An action that controls how and where App Services forwards logs.
  • A filter that controls which logs App Services forwards.
  • A policy that controls whether App Services batches logs or forwards them individually.

Consider setting up a log forwarder if you need to do any of the following:

  • Store logs for longer than App Services's retention period of 10 days.
  • Integrate logs into an external logging service
  • Access logs in Atlas Search, Online Archive, and Charts

Each log forward action invocation (on either an individual log or a batch) is billed as one App Services request.

App Services can forward all of your app's logs or send only a subset to the target collection or service. You control this subset for each log forwarder by defining filters for the log type (e.g. functions, sync, etc.) and status (i.e. success or error) that invoke the forwarder's action.

Important

App Services only forwards a given log if both its type and status are specified in the filter.

For example, consider a forwarder that filters for sync logs with an error status.

The filter would forward the following log:

{ "type": "sync", "status": "error", ... }

The filter would not forward the following logs:

{ "type": "sync", "status": "success", ... }
{ "type": "schema", "status": "error", ... }

App Services can combine multiple logs in to a single batched request to reduce overhead. The way that App Services groups logs into batches is controlled by a batching policy.

App Services supports the following batching policies:

  • No Batching: App Services forwards logs individually as their corresponding requests occur.
  • Batching: The forwarder groups documents into a batch as they happen. Each batch may include up to 100 log entries. When a batch is full, App Services forwards the entire batch in a single request. App Services forwards logs at least once a minute regardless of the number of logs in the current batch.

A log forwarder can automatically store logs in a linked MongoDB collection or call a custom function that sends the logs to an external service.

To forward logs to an external service, write a function that accepts an array of log objects and calls the service through an API, SDK, or library.

Note

Depending on your batching policy and log frequency, App Services may call a log forwarding function with an array of up to 100 log objects.

The function should have the same signature as the following example:

/functions/<function name>.js
exports = async function(logs) {
// `logs` is an array of 1-100 log objects
// Use an API or library to send the logs to another service.
await context.http.post({
url: "https://api.example.com/logs",
body: logs,
encodeBodyAsJSON: true
});
}

Once you've written the log forwarding function, you can assign it to a log forwarder by name.

A log forwarder may suspend in response to an event that prevents it from continuing, such as a network disruption or a change to the underlying cluster that stores the logs. Once suspended, a forwarder cannot be invoked and does not forward any logs.

You can restart a suspended log forwarder from the Logs > Forwarding screen of the Atlas App Services UI.

Note

If a log forwarder is suspended, App Services sends the project owner an email alerting them of the issue.

←  View Logs with Realm CLIAuthentication Logs →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.