Plugins

Logger

Comprehensive HTTP request/response logging with beautiful console output

The logger plugin provides detailed, structured logging for all HTTP request/response lifecycle events. Built on top of consola.

Features

  • Logs all HTTP requests and responses
  • Tracks errors and retries
  • Color-coded console output
  • Customizable logging options

Installation

npm install @zayne-labs/callapi-plugins

Quick Start

api.ts
import { createFetchClient } from "@zayne-labs/callapi";
import { loggerPlugin } from "@zayne-labs/callapi-plugins";

const callMainApi = createFetchClient({
	baseURL: "https://api.example.com",
	plugins: [
		loggerPlugin({
			enabled: process.env.NODE_ENV === "development",
			mode: process.env.DEBUG === "true" ? "verbose" : "basic",
		}),
	],
});

Configuration

enabled

Type: boolean | { onError?: boolean; onRequest?: boolean; onRequestError?: boolean; onResponse?: boolean; onResponseError?: boolean; onRetry?: boolean; onSuccess?: boolean; onValidationError?: boolean; } Default: true

Toggle logging on/off. Can be a boolean to enable/disable all logging, or an object for granular control over specific events.

// Simple boolean toggle
export const client1 = createFetchClient({
	plugins: [
		loggerPlugin({
			enabled: process.env.NODE_ENV === "development",
		}),
	],
});

// Granular control
export const client2 = createFetchClient({
	plugins: [
		loggerPlugin({
			enabled: {
				onRequest: true,
				onSuccess: true,
				onError: true, // Enable all error logging by default
				onValidationError: false, // Disable validation error logging specifically
			},
		}),
	],
});

// Alternative: Enable only specific error types
export const client3 = createFetchClient({
	plugins: [
		loggerPlugin({
			enabled: {
				onRequest: true,
				onSuccess: true,
				onRequestError: true, // Only network/request errors
				onResponseError: false, // Disable HTTP error logging
				onValidationError: false, // Disable validation error logging
			},
		}),
	],
});

consoleObject

Type: ConsoleLikeObject Default: A pre-configured consola instance

Allows you to provide a custom console-like object that implements the following interface:

interface ConsoleLikeObject {
	error: (...args: any[]) => void;
	fail?: (...args: any[]) => void;
	log: (...args: any[]) => void;
	success?: (...args: any[]) => void;
	warn?: (...args: any[]) => void;
}

Whatever you provide will be used instead of the default consola instance.

api.ts
import { loggerPlugin, type ConsoleLikeObject } from "@zayne-labs/callapi-plugins";

const customLogger: ConsoleLikeObject = {
	log: (...args) => console.log("[API]", ...args),
	error: (...args) => console.error("[API ERROR]", ...args),
	warn: (...args) => console.warn("[API WARN]", ...args),
	success: (...args) => console.log("[API SUCCESS]", ...args),
};

export const client = createFetchClient({
	plugins: [loggerPlugin({ consoleObject: customLogger })],
});

mode

Type: "basic" | "verbose" Default: "basic"

Controls the verbosity of logging output:

  • "basic": Standard logging with essential information
  • "verbose": Detailed debugging with full payloads, error data, and additional context
export const client = createFetchClient({
	plugins: [loggerPlugin({ mode: "verbose" })],
});

Logged Events

The logger plugin will log the following events:

  • Request Started: When a request is initiated
  • Request Error: When a request fails to be sent
  • Response Error: When a response is received with an error status
  • Retry: When a request is being retried (with attempt count)
  • Success: When a request completes successfully
  • Validation Error: When request/response validation fails (schema validation)
Edit on GitHub

Last updated on

On this page