日志器链接
loggerLink
是一个链接,可让你为 tRPC 客户端实现日志器。它使你可以更清楚地看到哪些操作是查询、突变或订阅、它们的请求和响应。默认情况下,该链接会将美化的日志打印到浏览器的控制台。但是,你可以使用自己的实现来自定义日志记录行为及其打印到控制台的方式。
¥loggerLink
is a link that lets you implement a logger for your tRPC client. It allows you to see more clearly what operations are queries, mutations, or subscriptions, their requests, and responses. The link, by default, prints a prettified log to the browser's console. However, you can customize the logging behavior and the way it prints to the console with your own implementations.
用法
¥Usage
你可以将 loggerLink
导入并添加到 links
数组中,如下所示:
¥You can import and add the loggerLink
to the links
array as such:
client/index.tsts
import { createTRPCProxyClient, httpBatchLink, loggerLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCProxyClient<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.tsts
import { createTRPCProxyClient, httpBatchLink, loggerLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCProxyClient<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',}),],});
loggerLink
选项
¥loggerLink
Options
loggerLink
函数采用具有 LoggerLinkOptions
形状的选项对象:
¥The loggerLink
function takes an options object that has the LoggerLinkOptions
shape:
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';};
参考
¥Reference
你可以在 GitHub。 上查看此链接的源代码
¥You can check out the source code for this link on GitHub.