In this blog post we will cover how the recently launched support for structured logging in OCI Functions can be used to enrich the operational experience of using functions. Structured logging support allows you to preserve the structure of JSON-formatted logs emitted by your function code, enabling improved viewing and aggregation of log lines.

Background

Since launch, OCI Functions has supported integration with OCI Service Logs, allowing users to view logs emitted by their function code in the OCI Logging service. When enabled, log lines emitted via stdout/stderr appear in the OCI Logging service as strings.

A screenshot showing a plain text string in the 'message' field in the OCI Logging UI

This works well enough for functions which emit plain text log lines, as in the above example. However, we are limited to just a single string field, and simple text search on that field.

In order to provide richer logs, modern applications may typically emit a JSON object per log line. This object can include multiple fields, upon which an operator may choose to filter. For example, it is typical for a logging framework to include a ‘level’ field, indicating the severity of that particular log line.

Previously, even if your function code emitted log lines in this structured format, the log line would appear in OCI Logging as a string, with any JSON escaped, limiting its usefulness.

A screenshot showing a an escaped JSON object in a string in the 'message' field in the OCI Logging UI

With the launch of structured logging support, it is now possible to configure the ‘logging line format’ on an application. There are two supported values:

  • Plain text – This is the existing behavior, shown above. Log lines from any functions within the application appear as strings in OCI Logging. This remains the default mode.
  • JSON – This indicates that log lines should be treated as JSON. Log lines from any functions withing the application that are valid JSON, are propagated as JSON to OCI Logging. Any lines which are not valid JSON still get propagated as strings.

Getting Started

Structured logging support can be configured via the regular mechanisms – OCI SDKs, CLI, Terraform, and the OCI console.

Console Example

  1. Open the Functions service in the OCI Console.
  2. Select your application.
  3. Under Application Information, click Edit on the Logging line format row.
  4. Choose JSON and click Update.
A screenshot showing the application details page in the OCI Console, with the 'Logging line format' field highlighted
A screenshot showing the 'Edit log line format' modal in the OCI Console, with options of 'Plain text' or 'JSON'

CLI Example

New application

oci fn application create \
  --compartment-id <COMPARTMENT> \
  --display-name <NAME> \
  --subnet-ids '["<SUBNET>"]' \
  --logging '{"lineFormat":"JSON"}'

Update existing application

oci fn application update \
  --application-id <APPLICATION> \
  --logging '{"lineFormat":"JSON"}'

Viewing Logs

With the logging line format of our application configured to JSON, we can now explore our custom log fields within each line in the Logging Search UI:

A screenshot showing an expanded native JSON object in the 'message' field in the OCI Logging UI

Within the ‘message’ field, you can see fields from the JSON log line emitted by my function, including ‘level’ and ‘myCustomerId’. Using the Logging Search UI, we can add columns to our summary view:

A screenshot showing the right-click menu for a field in the OCI Logging UI, with the 'Add to summary view' button highlighted

This allows us to build a view focused on fields relevant to our application:

A screenshot showing the summary view in the OCI Logging UI, with several columns for application-specific log fields

We can also easily filter by value for a given field:

A screenshot showing the right-click menu for a field in the OCI Logging UI, with the 'Filter matching' button highlighted

This allows us to focus on e.g. log lines pertaining to a given customer ID:

A screenshot showing the summary view in the OCI Logging UI, with several columns for application-specific log fields. Only log lines where the value of the 'myCustomerId' field is 'john' are shown

For more details on advanced log querying, see the OCI Logging documentation.

Try It Today

Functions structured logging support is now available in all commercial OCI regions at no additional cost. With this functionality, you can finally leverage JSON-format logs from your functions to surface structured data for filtering and searching. Get started with OCI Functions today and see how these new features can improve the operational experience of running functions.

To learn more, see the following resources: