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: LogEvent,
  retryConfig?: ContextFunctionRetryConfig
): Promise<void>
Where LogEvent is:
export type LogEvent = {
  code: string;
  message: string;
  data: string;
  task?: string;
  runId?: string;
};
And ContextFunctionRetryConfig is:
type ContextFunctionRetryConfig = {
  max_attempts: number;
  initial_interval_ms: number;
  backoff_factor: number;
};

Examples

Basic Event Logging

import { TaskContext } from "compose";

export async function main({ logEvent, fetch }: TaskContext) {
  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