React Native 로그 수집
Datadog의 dd-sdk-reactnative 클라이언트 측 로깅 라이브러리를 사용해 React Native 모바일 애플리케이션에서 Datadog로 로그를 보내고 다음 기능을 활용할 수 있어요:
- JSON 형식으로 기본 제공되는 형태로 Datadog에 로그 기록.
- 보내는 모든 로그에
context와 추가 사용자 정의 속성 추가. - JavaScript에서 잡은 예외 전달.
- 실제 클라이언트 IP 주소와 User-Agent 기록.
- 자동 벌크 게시로 최적화된 네트워크 사용량.
출처: 문서
본문
설정
@datadog/mobile-react-native패키지를 설치하세요.
NPM으로 설치하려면 다음을 실행하세요:
npm install @datadog/mobile-react-native
Yarn으로 설치하려면 다음을 실행하세요:
yarn add @datadog/mobile-react-native
그런 다음 추가된 pod를 설치하세요:
(cd ios && pod install)
버전 1.0.0-rc5 이상은 Android 애플리케이션 설정에 compileSdkVersion = 31이 필요해요. 즉 Build Tools 버전 31, Android Gradle Plugin 버전 7, Gradle 버전 7 이상을 사용해야 해요. 버전을 수정하려면 애플리케이션 최상위 build.gradle 파일의 buildscript.ext 블록 값을 바꾸세요. Datadog는 React Native 버전 0.67 이상을 권장해요.
애플리케이션 컨텍스트, 추적 동의, Datadog 클라이언트 토큰, 그리고 Datadog UI에서 RUM 애플리케이션을 만들 때 생성한 Application ID로 라이브러리를 초기화하세요(자세한 내용은 React Native RUM 수집 시작하기 참고). 보안상의 이유로 클라이언트 토큰을 사용해야 해요. Datadog API 키는 모바일 애플리케이션에서 클라이언트 측에 노출되므로 dd-sdk-reactnative 라이브러리 구성에 사용할 수 없어요. 클라이언트 토큰 설정에 대한 자세한 내용은 클라이언트 토큰 문서를 참고하세요.
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: app.datadoghq.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'US1',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: us3.datadoghq.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'US3',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: us5.datadoghq.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'US5',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: app.datadoghq.eu
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'EU1',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: app.ddog-gov.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'US1_FED',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: us2.ddog-gov.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'US2_FED',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: ap1.datadoghq.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'AP1',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: ap2.datadoghq.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'AP2',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
{% callout %}
다음 Datadog 사이트 사용자를 위한 중요 참고 사항: uk1.datadoghq.com
import { DatadogProviderConfiguration } from '@datadog/mobile-react-native';
const config = new DatadogProviderConfiguration(
'<CLIENT_TOKEN>',
'<ENVIRONMENT_NAME>',
trackingConsent,
{
site: 'UK1',
rumConfiguration: {
applicationId: '<APPLICATION_ID>', // RUM Application ID
trackInteractions: true, // Track user interactions (set to false if using Error Tracking only)
trackResources: true, // Track XHR resources (set to false if using Error Tracking only)
trackErrors: true, // Track errors
},
logsConfiguration: {}, // Enable Logs
traceConfiguration: {} // Enable Traces
}
);
{% /callout %}
React Native 로거를 가져오세요:
import { DdLogs } from '@datadog/mobile-react-native';
다음 함수 중 하나로 사용자 정의 로그 항목을 Datadog로 직접 보내세요:
DdLogs.debug('A debug message.', { customAttribute: 'something' })
DdLogs.info('Some relevant information ?', { customCount: 42 })
DdLogs.warn('An important warning…', {})
DdLogs.error('An error was met!', {})
참고: 모든 로깅 메서드는 사용자 정의 속성이 있는 컨텍스트 객체를 가질 수 있어요.
배치 수집
모든 로그는 먼저 로컬 디바이스에 배치로 저장돼요. 각 배치는 수집 규격을 따르며, 네트워크가 사용 가능하고 배터리가 충분해 Datadog SDK가 최종 사용자 경험에 영향을 주지 않을 때 보내져요. 애플리케이션이 포그라운드에 있을 때 네트워크를 사용할 수 없거나 데이터 업로드가 실패하면, 배치는 성공적으로 보낼 수 있을 때까지 유지돼요.
즉, 사용자가 오프라인 상태에서 애플리케이션을 열어도 데이터는 손실되지 않아요.
SDK가 디스크 공간을 과도하게 사용하지 않도록, 디스크의 데이터는 너무 오래되면 자동으로 폐기돼요.
사용자 정의 오류 지문
v2.4.2부터 fingerprint 인자를 사용해 오류 로그에 사용자 정의 지문을 추가할 수 있어요:
export type LogWithErrorArguments = [
message: string,
errorKind?: string,
errorMessage?: string,
stacktrace?: string,
context?: object,
fingerprint?: string
];