| import * as Sentry from '@sentry/react'; | |
| let sentryReady = false; | |
| export function initLogger() { | |
| const dsn = import.meta.env.VITE_SENTRY_DSN as string | undefined; | |
| if (!dsn) return; | |
| Sentry.init({ | |
| dsn, | |
| environment: import.meta.env.MODE, | |
| tracesSampleRate: 0.1, | |
| replaysOnErrorSampleRate: 1.0, | |
| }); | |
| sentryReady = true; | |
| } | |
| export function logError(messageOrErr: unknown, err?: unknown) { | |
| console.error(messageOrErr, err ?? ''); | |
| if (sentryReady) { | |
| const captured = messageOrErr instanceof Error ? messageOrErr | |
| : err instanceof Error ? err | |
| : new Error(typeof messageOrErr === 'string' ? messageOrErr : 'Unknown error'); | |
| Sentry.withScope(scope => { | |
| if (err && !(err instanceof Error)) scope.setExtra('details', String(err)); | |
| Sentry.captureException(captured); | |
| }); | |
| } | |
| } | |
| export function logWarn(message: unknown, err?: unknown) { | |
| console.warn(message, err ?? ''); | |
| if (sentryReady) { | |
| const msg = typeof message === 'string' ? message : String(message); | |
| Sentry.withScope(scope => { | |
| if (err) scope.setExtra('details', String(err)); | |
| Sentry.captureMessage(msg, 'warning'); | |
| }); | |
| } | |
| } | |