Storing and Viewing Function Logs

Find out how to store and view function logs with OCI Functions.

When a function is invoked, you'll typically want to access the function's logs for troubleshooting. The Oracle Cloud Infrastructure Logging service is the default and recommended option for accessing, searching, and storing function logs. If you enable Oracle Cloud Infrastructure Logging for an application, default invocation logs are created whenever functions in the application are invoked. See Using the Console to Enable and View Function Logs in Oracle Cloud Infrastructure Logging. For more information about the contents of function logs, see Details for Functions.

Alternatively, there might be occasions when you want to send function logs to an external logging destination like Papertrail. To send logs to an external logging destination instead of the Oracle Cloud Infrastructure Logging service, you use the Fn Project CLI to specify a syslog URL. See Using Fn Project CLI Commands to Specify a syslog URL.

Note that to store and view logs for a function (other than default invocation logs stored in the Oracle Cloud Infrastructure Logging service), the function must include print statements. For example:
  • For node.js: console.log('Entering Hello Node.js function');
  • For java: System.err.println("Entering Java Hello World Function");
  • For go: fmt.Println("Entering Hello Go function")

Using the Console to Enable and View Function Logs in Oracle Cloud Infrastructure Logging

To enable and view function logs in the Oracle Cloud Infrastructure Logging service:

  1. Sign in to the Console as a functions developer.
  2. Open the navigation menu and click Developer Services. Under Functions, click Applications.
  3. Select the region and compartment containing the application with functions for which you want to create, enable, and view logs.

    The Applications page shows all the applications in the compartment you selected.

  4. Select the application with functions for which you want to create, enable, and view logs.
  5. To create and enable a new function log in the Oracle Cloud Infrastructure Logging service:
    1. Under Resources, click Logs, click the Actions menu (Actions Menu), and then click Enable Log and specify:
      • Compartment: The compartment in which to create the new log. By default, the current compartment.
      • Log Group: The log group in which to create the new log. Select an existing log group, or select:
        • Auto-create a default log group to create a default log group with a default name (DEFAULT_GROUP), if one doesn't exist already.
        • Create a new log group to create a new log group with a name and description that you provide.
      • Log Name: The name of the new log. By default, <application-name>_invoke .
      • Log Retention: The length of time to retain log data.
    2. Click Enable Log to create the new log (and the new log group, if you specified one).

    For more information, see Enabling Logging for a Resource.

  6. To enable an existing function log, under Resources, click Logs, click the Actions menu (Actions Menu), and then click Enable Log.
  7. To view the data in an existing function log, under Resources, click Logs, and then click the name of the log group containing the log you want to view in the Log Group column.

    The log opens in the log group's Log Details page, enabling you to view log data, and sort and filter log data by time.

  8. Click the down arrow beside a log event to see log data for that event.
  9. Click Explore with Log Search to search log data.

Tip

When searching log data, you can use the data.opcRequestId field to filter logs by request id. If an API deployment in API Gateway invokes a function on an OCI Functions back end, you can see logs relating to both the API Gateway request and the OCI Functions request by using the same data.opcRequestId field together with a wildcard, as follows:

  1. On the Logging Search page, click the down arrow beside a log event to see log data for that event.
  2. Click the "data.opcRequestId" field in the function log and select Filter matching from the popup menu.

    For example, assume the function log contains the field "data.opcRequestId": "/01FJA5VCVM0000000000025M1Z/01FJA5VCVM0000000000025M20". The field "data.opcRequestId": "/01FJA5VCVM0000000000025M1Z/01FJA5VCVM0000000000025M20" is copied into the Log Search (Basic Mode) page as a filter.

  3. On the Log Search (Basic Mode) page, click Show Advanced Mode and replace the second half of the "data.opcRequestId" field value shown in the Query field with the * (asterisk) wildcard.

    For example, so that it reads data.opcRequestId: '/01FJA5VCVM0000000000025M1Z/*'

    This image shows part of the Log Search (Advanced Mode) page, with a wildcard as the second half of the "data.opcRequestId" field.
  4. Click Search.

Using Fn Project CLI Commands to Specify a syslog URL

Tip

From time to time, new versions of the Fn Project CLI are released. We recommend you regularly check that the latest version is installed. For more information, see Steps to upgrade the Fn Project CLI.

The Oracle Cloud Infrastructure Logging service is the default and recommended option for accessing, searching, and storing function logs.

Alternatively, you can send function logs to an external logging destination like Papertrail instead by using the Fn Project CLI to specify a syslog URL. Note that to use an external logging destination, you must have set up a VCN with public subnets and an internet gateway (see Creating the VCN and Subnets to Use with OCI Functions, if they don't exist already).

To send function logs to an external logging destination by setting the syslog URL:

  1. Log in to your development environment as a functions developer.
  2. To create a new application and specify that all functions in the application send their logs to an external logging destination, enter:

    fn create app <app-name> --syslog-url <logging-service-url> --annotation oracle.com/oci/subnetIds='["<subnet-ocid>"]'

    where:

    • <app-name> is the name of the new application. Avoid entering confidential information.
    • <logging-service-url> is the syslog URL to which to send logs.
    • <subnet-ocid> is the OCID of the public subnet (or subnets, up to a maximum of six) in which to run functions. If a regional subnet has been defined, best practice is to select that subnet to make failover across availability domains simpler to implement. If a regional subnet has not been defined and you need to meet high availability requirements, select multiple subnets (enclose each OCID in double quotes separated by commas, in the format '["<subnet-ocid>","<subnet-ocid>"]'. We recommend that the public subnets are in the same region as the Docker registry that's specified in the Fn Project CLI context (see Creating an Fn Project CLI Context to Connect to Oracle Cloud Infrastructure).

    For example:

    fn create app acmeapp --syslog-url tcp://my.papertrail.com:4242 --annotation oracle.com/oci/subnetIds='["ocid1.subnet.oc1.phx.aaaaaaaacnh..."]'

    Note that if you subsequently set up Oracle Cloud Infrastructure Logging to store logs, the existing syslog URL details are retained. So if you later decide to resume sending function logs to the external logging destination, you simply have to disable Oracle Cloud Infrastructure Logging and logs will be sent to the syslog URL again.

  3. To update an existing application and specify that all functions in the application send their logs to an external logging destination, enter:

    fn update app <app-name> --syslog-url <logging-service-url> 

    where:

    • <app-name> is the name of the application to update
    • <logging-service-url> is the syslog URL to which to send logs

    For example:

    fn update app acmeapp --syslog-url tcp://my.papertrail.com:4242
  4. To update an existing application and remove the external logging destination specified for the syslog URL, enter:

    fn update app <app-name> --syslog-url '' 

    where:

    • <app-name> is the name of the application to update

    For example:

    fn update app acmeapp --syslog-url ''

Previously Supported Logging Options

In earlier OCI Functions releases (prior to the release of the Oracle Cloud Infrastructure Logging service), you could specify where OCI Functions stores a function's logs by setting up a 'logging policy' for the application containing the function. Previously, you could use the Console to set up a logging policy to:

  • Store logs as objects in a storage bucket in Oracle Cloud Infrastructure Object Storage by selecting the OCI Logging option.

    To view function logs in a storage bucket, the group to which you belong must have been granted access with the following identity policy statements:

  • Store logs by sending them to an external logging destination like Papertrail by selecting the Syslog URL option.

For an existing application where you have previously already set up a logging policy, the above functionality is still supported and the existing logging policy is applied. However, note the following:

  • You cannot use the Console to set up a new logging policy or edit an existing logging policy.
  • If the existing logging policy specified storing function logs as objects in a storage bucket in Oracle Cloud Infrastructure Object Storage:
    • The ability to store logs in Object Storage will be deprecated in a future release.
    • We recommend you switch to storing logs using Oracle Cloud Infrastructure Logging.
    • If you do switch to using Oracle Cloud Infrastructure Logging to store logs, you cannot revert to storing the logs in Object Storage.
    • Logs stored in Object Storage will continue to exist (with each log name including the OCID of the associated function, as before).
  • If the existing logging policy specified a syslog URL:
    • If you switch to using Oracle Cloud Infrastructure Logging to store logs, the existing syslog URL details are retained. So if you later decide to resume sending function logs to the external logging destination, you simply have to disable Oracle Cloud Infrastructure Logging and logs will be sent to the syslog URL again.
    • If you want to change the syslog URL in the existing logging policy, you have to use the Fn Project CLI to change it.