Custom logging

By default, SDKs output info and error logs to the console.

You can provide a custom logging callback to capture and forward logs elsewhere, e.g. to your monitoring infrastructure, by setting logsHandler in your createSource options:

src/lib/getHypertune.ts
import { createSource } from "../generated/hypertune";

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  logsHandler: (logs) => {
    logs.messageList.forEach(({ level, message, metadata }) =>
      console.log(level, message, metadata)
    );
  },
});

export default async function getHypertune() {
  // Get flag updates in serverless environments
  // await hypertuneSource.initIfNeeded();

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === "development"
            ? "development"
            : "production",
        user: { id: "1", name: "Test", email: "hi@test.com" },
      },
    },
  });
}

logsHandler?: (logs: Logs) => void;

type Logs {
  messageList: Message[];
  ...
}

export type Message = {
  level: LogLevel;
  message: string;
  metadata: object;
}

enum LogLevel {
  Debug = 'Debug',
  Error = 'Error',
  Info = 'Info',
  Warn = 'Warn'
}

Last updated