Saltar al contenido principal
Versión: 11.x

Enlace de Registro

Traducción Beta No Oficial

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

loggerLink es un enlace que te permite implementar un sistema de registro para tu cliente tRPC. Te ayuda a identificar claramente qué operaciones son queries, mutations o subscriptions, mostrando tanto las solicitudes como las respuestas. Por defecto, este enlace imprime registros formateados en la consola del navegador. Sin embargo, puedes personalizar completamente el comportamiento del registro y cómo se muestra en consola con tus propias implementaciones.

Uso

Puedes importar y agregar el loggerLink al array de links de esta manera:

client/index.ts
ts
import { createTRPCClient, httpBatchLink, loggerLink } from '@trpc/client';
import type { AppRouter } from '../server';
const client = createTRPCClient<AppRouter>({
links: [
/**
* The function passed to enabled is an example in case you want to the link to
* log to your console in development and only log errors in production
*/
loggerLink({
enabled: (opts) =>
(process.env.NODE_ENV === 'development' &&
typeof window !== 'undefined') ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({
url: 'http://localhost:3000',
}),
],
});
client/index.ts
ts
import { createTRPCClient, httpBatchLink, loggerLink } from '@trpc/client';
import type { AppRouter } from '../server';
const client = createTRPCClient<AppRouter>({
links: [
/**
* The function passed to enabled is an example in case you want to the link to
* log to your console in development and only log errors in production
*/
loggerLink({
enabled: (opts) =>
(process.env.NODE_ENV === 'development' &&
typeof window !== 'undefined') ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({
url: 'http://localhost:3000',
}),
],
});

La función loggerLink recibe un objeto de opciones con la estructura de LoggerLinkOptions:

ts
type LoggerLinkOptions<TRouter extends AnyRouter> = {
logger?: LogFn<TRouter>;
/**
* It is a function that returns a condition that determines whether to enable the logger.
* It is true by default.
*/
enabled?: EnabledFn<TRouter>;
/**
* Used in the built-in defaultLogger
*/
console?: ConsoleEsque;
/**
* Color mode used in the default logger.
* @default typeof window === 'undefined' ? 'ansi' : 'css'
*/
colorMode?: 'ansi' | 'css';
};
ts
type LoggerLinkOptions<TRouter extends AnyRouter> = {
logger?: LogFn<TRouter>;
/**
* It is a function that returns a condition that determines whether to enable the logger.
* It is true by default.
*/
enabled?: EnabledFn<TRouter>;
/**
* Used in the built-in defaultLogger
*/
console?: ConsoleEsque;
/**
* Color mode used in the default logger.
* @default typeof window === 'undefined' ? 'ansi' : 'css'
*/
colorMode?: 'ansi' | 'css';
};

Referencia

Puedes consultar el código fuente de este enlace en GitHub.