본문 바로가기
버전: 11.x

로거 링크

비공식 베타 번역

이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →

loggerLink는 tRPC 클라이언트에 로거를 구현할 수 있게 해주는 링크입니다. 이를 통해 쿼리, 변이 또는 구독 작업과 해당 요청 및 응답을 더 명확하게 확인할 수 있습니다. 기본적으로 이 링크는 브라우저 콘솔에 보기 좋게 정리된 로그를 출력하지만, 로깅 동작과 콘솔 출력 방식을 사용자 정의 구현으로 변경할 수 있습니다.

사용법

다음과 같이 loggerLink를 import하여 links 배열에 추가할 수 있습니다:

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',
}),
],
});

loggerLink 함수는 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';
};

참조

이 링크의 소스 코드는 GitHub에서 확인할 수 있습니다.