Skip to main content

Event Logging - logEvent

Log structured events to the events storage for custom Audit trails. By default all context functions will leave an auditable event trail, but you can add custom events to flesh out the audit trail.
logEvent(
  event: {
    code: string;
    message: string;
    data: string;
  },
  retryConfig?: HostFunctionRetryConfig
): Promise<void>

Examples

Basic Event Logging

/// <reference types="../../.compose/types.d.ts" />

export async function main({ logEvent, fetch }) {
  await logEvent({
    code: "TASK_STARTED",
    message: "Data processing task initiated",
    data: JSON.stringify({ timestamp: Date.now() }),
  });

  try {
    const data = await fetch("https://api.example.com/data");

    await logEvent({
      code: "DATA_FETCHED",
      message: `Successfully fetched ${data.length} records`,
      data: JSON.stringify({
        recordCount: data.length,
        source: "api.example.com",
      }),
    });

    return { success: true, records: data.length };
  } catch (error) {
    await logEvent({
      code: "FETCH_ERROR",
      message: `Failed to fetch data: ${error.message}`,
      data: JSON.stringify({
        error: error.message,
        stack: error.stack,
        url: "https://api.example.com/data",
      }),
    });

    throw error;
  }
}

Next Steps