logo

Vedatrace

Javascript sdk

API Reference

The complete API reference for the VedaTrace SDK for JavaScript and TypeScript.

vedatrace(config)

Creates a new logger instance. See the Configuration page for all available options.

logger.debug(message, metadata?)

Logs a message at the debug level.

  • message (string): The message to log.
  • metadata (object, optional): Additional data to include with the log.

logger.info(message, metadata?)

Logs a message at the info level.

  • message (string): The message to log.
  • metadata (object, optional): Additional data to include with the log.

logger.warn(message, metadata?)

Logs a message at the warn level.

  • message (string): The message to log.
  • metadata (object, optional): Additional data to include with the log.

logger.error(message | Error, metadata?)

Logs a message at the error level.

  • message (string | Error): The message or Error object to log.
  • metadata (object, optional): Additional data to include with the log.

logger.fatal(message | Error, metadata?)

Logs a message at the fatal level.

  • message (string | Error): The message or Error object to log.
  • metadata (object, optional): Additional data to include with the log.

logger.child(defaults)

Creates a new child logger instance with default metadata.

  • defaults (object): An object of default metadata to include with all logs from this child logger.

logger.flush()

Manually flushes the log buffer, sending all pending logs to the configured transports. Returns a Promise that resolves when the flush is complete.

In Cloudflare Workers, it's recommended to pass waitUntil in the config instead of calling logger.flush() manually, so flushing happens automatically.

logger.stop()

Stops the background timer that automatically flushes logs. This is useful for graceful shutdown in long-running applications.

logger.start()

Manually starts the background timer that flushes logs.

logger.withContext(ctx)

Attaches a Cloudflare ExecutionContext to the logger. The SDK uses ctx.waitUntil() to keep the worker alive during log flushing.

const logger = vedatrace({
  apiKey: env.VEDATRACE_API_KEY,
  service: 'my-worker',
})

export default {
  async fetch(request, env, ctx) {
    logger.withContext(ctx)
    logger.info('Request received')
    return new Response('OK')
  }
}

logger.runtime

Returns the detected runtime environment. Returns one of: 'node', 'browser', 'cloudflare', 'deno', 'bun', 'edge'.

const logger = vedatrace({ apiKey: 'xxx' })
console.log(logger.runtime) // 'node', 'browser', 'cloudflare', 'deno', 'bun', or 'edge'

Configuration Options

The vedatrace(config) function accepts the following configuration options:

OptionTypeDefaultDescription
apiKeystring—Your VedaTrace API key
servicestring—Default service name for all logs
endpointstringhttps://ingest.vedatrace.dev/v1/logsCustom ingestion endpoint
environmentstring—Deployment environment (e.g. production, development)
batchSizenumber100Number of logs to buffer before flushing
flushIntervalnumber5000 (Node/Browser) or 1000 (edge)Max time (ms) between flushes
immediateFlushbooleanfalseFlush immediately on each log, bypassing batching
waitUntilfunction—Cloudflare Workers waitUntil for automatic flush
maxRetriesnumber3Max retries per batch on transport failure
retryDelaynumber1000Base delay (ms) between retries
redactionobject—Paths and mask for sensitive data redaction
debugbooleanfalseEnable verbose SDK debug logging
onErrorfunction—Called when log delivery fails
onSuccessfunction—Called when logs are delivered successfully
autoStartbooleantrue (Node/Browser), false (edge)Auto-start the flush timer
runtimeRuntimeTypeauto-detectedForce a specific runtime

Runtime Detection Functions

VedaTrace also exports runtime detection functions:

detectRuntime()

Returns the detected runtime environment as a string.

import { detectRuntime } from 'vedatrace'

const runtime = detectRuntime()
// Returns: 'node' | 'browser' | 'cloudflare' | 'deno' | 'bun' | 'edge'

isEdgeRuntime()

Returns true if the current runtime is an edge runtime (Cloudflare Workers, Deno, Bun, or edge).

import { isEdgeRuntime } from 'vedatrace'

if (isEdgeRuntime()) {
  console.log('Running on edge runtime')
}

On this page