ロガーリンク
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
loggerLinkは、tRPCクライアント用のロガーを実装できるリンクです。このリンクを使用すると、クエリ・ミューテーション・サブスクリプションといった操作と、それらのリクエスト・レスポンスをより明確に確認できます。デフォルトではブラウザのコンソールに整形されたログを出力しますが、ログの動作やコンソールへの出力方法を独自実装でカスタマイズ可能です。
使用方法
loggerLinkは次のようにインポートしてlinks配列に追加できます:
client/index.tstsimport { 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.tstsimport { 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',}),],});
loggerLinkのオプション
loggerLink関数はLoggerLinkOptions形式のオプションオブジェクトを受け取ります:
tstype 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';};
tstype 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';};
参考
このリンクのソースコードはGitHubで確認できます。