Skip to content

Commit bac05e6

Browse files
authored
feat: add interface for logger (#150)
1 parent 82aa887 commit bac05e6

File tree

4 files changed

+138
-35
lines changed

4 files changed

+138
-35
lines changed

package-lock.json

Lines changed: 100 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
]
5353
},
5454
"dependencies": {
55-
"@sentry/browser": "7.68.0",
55+
"@sentry/browser": "7.109.0",
56+
"@sentry/integrations": "7.109.0",
5657
"@tanstack/react-query": "4.36.1",
5758
"@tanstack/react-query-devtools": "4.35.0",
5859
"axios": "1.6.8",

src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import './wdyr';
22
import React from 'react';
33
import { createRoot } from 'react-dom/client';
4-
import * as Sentry from '@sentry/browser';
54
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
6-
import 'assets/styles/main.css';
75

6+
import 'assets/styles/main.css';
87
import { AppProviders } from 'providers/AppProviders';
98
import { AppRoutes } from 'routing/AppRoutes';
109
import { enableMocking } from 'setupMSW';
10+
import { logger } from 'integrations/logger';
1111

1212
const openReactQueryDevtools = import.meta.env.DEV;
1313

1414
if (import.meta.env.VITE_SENTRY_DSN) {
15-
Sentry.init({ dsn: import.meta.env.VITE_SENTRY_DSN });
15+
logger.init();
1616
}
1717

1818
const container = document.getElementById('root');

src/integrations/logger.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { init, captureException, captureMessage, browserTracingIntegration } from '@sentry/browser';
2+
import { httpClientIntegration, captureConsoleIntegration } from '@sentry/integrations';
3+
4+
type LogLevel = 'error' | 'info' | 'warning';
5+
type Logger = Record<LogLevel, (message: string | Error) => void> & Record<string, unknown>;
6+
7+
const initLogger = () =>
8+
init({
9+
dsn: import.meta.env.VITE_SENTRY_DSN,
10+
integrations: [
11+
httpClientIntegration({
12+
failedRequestStatusCodes: [[400, 599]],
13+
failedRequestTargets: [/.*/],
14+
}),
15+
captureConsoleIntegration(),
16+
browserTracingIntegration(),
17+
],
18+
tracesSampleRate: 1.0,
19+
});
20+
21+
const sendLog = (level: LogLevel, message: string | Error) => {
22+
if (typeof message === 'string') {
23+
captureMessage(message, { level });
24+
}
25+
captureException(message, { level });
26+
};
27+
28+
export const logger = {
29+
init: initLogger,
30+
error: (message: string | Error) => sendLog('error', message),
31+
warning: (message: string | Error) => sendLog('warning', message),
32+
info: (message: string | Error) => sendLog('info', message),
33+
} satisfies Logger;

0 commit comments

Comments
 (0)