|
| 1 | +import type { Log, LogSeverityLevel, ParameterizedString } from '@sentry/core'; |
| 2 | +import { _INTERNAL_captureLog } from '@sentry/core'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Capture a log with the given level. |
| 6 | + * |
| 7 | + * @param level - The level of the log. |
| 8 | + * @param message - The message to log. |
| 9 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 10 | + * @param severityNumber - The severity number of the log. |
| 11 | + */ |
| 12 | +function captureLog( |
| 13 | + level: LogSeverityLevel, |
| 14 | + message: ParameterizedString, |
| 15 | + attributes?: Log['attributes'], |
| 16 | + severityNumber?: Log['severityNumber'], |
| 17 | +): void { |
| 18 | + _INTERNAL_captureLog({ level, message, attributes, severityNumber }); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @summary Capture a log with the `trace` level. Requires `_experiments.enableLogs` to be enabled. |
| 23 | + * |
| 24 | + * @param message - The message to log. |
| 25 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., { userId: 100, route: '/dashboard' }. |
| 26 | + * |
| 27 | + * @example |
| 28 | + * |
| 29 | + * ``` |
| 30 | + * Sentry.logger.trace('User clicked submit button', { |
| 31 | + * buttonId: 'submit-form', |
| 32 | + * formId: 'user-profile', |
| 33 | + * timestamp: Date.now() |
| 34 | + * }); |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * @example With template strings |
| 38 | + * |
| 39 | + * ``` |
| 40 | + * Sentry.logger.trace(Sentry.logger.fmt`User ${user} navigated to ${page}`, { |
| 41 | + * userId: '123', |
| 42 | + * sessionId: 'abc-xyz' |
| 43 | + * }); |
| 44 | + * ``` |
| 45 | + */ |
| 46 | +export function trace(message: ParameterizedString, attributes?: Log['attributes']): void { |
| 47 | + captureLog('trace', message, attributes); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @summary Capture a log with the `debug` level. Requires `_experiments.enableLogs` to be enabled. |
| 52 | + * |
| 53 | + * @param message - The message to log. |
| 54 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., { component: 'Header', state: 'loading' }. |
| 55 | + * |
| 56 | + * @example |
| 57 | + * |
| 58 | + * ``` |
| 59 | + * Sentry.logger.debug('Component mounted', { |
| 60 | + * component: 'UserProfile', |
| 61 | + * props: { userId: 123 }, |
| 62 | + * renderTime: 150 |
| 63 | + * }); |
| 64 | + * ``` |
| 65 | + * |
| 66 | + * @example With template strings |
| 67 | + * |
| 68 | + * ``` |
| 69 | + * Sentry.logger.debug(Sentry.logger.fmt`API request to ${endpoint} failed`, { |
| 70 | + * statusCode: 404, |
| 71 | + * requestId: 'req-123', |
| 72 | + * duration: 250 |
| 73 | + * }); |
| 74 | + * ``` |
| 75 | + */ |
| 76 | +export function debug(message: ParameterizedString, attributes?: Log['attributes']): void { |
| 77 | + captureLog('debug', message, attributes); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @summary Capture a log with the `info` level. Requires `_experiments.enableLogs` to be enabled. |
| 82 | + * |
| 83 | + * @param message - The message to log. |
| 84 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., { feature: 'checkout', status: 'completed' }. |
| 85 | + * |
| 86 | + * @example |
| 87 | + * |
| 88 | + * ``` |
| 89 | + * Sentry.logger.info('User completed checkout', { |
| 90 | + * orderId: 'order-123', |
| 91 | + * amount: 99.99, |
| 92 | + * paymentMethod: 'credit_card' |
| 93 | + * }); |
| 94 | + * ``` |
| 95 | + * |
| 96 | + * @example With template strings |
| 97 | + * |
| 98 | + * ``` |
| 99 | + * Sentry.logger.info(Sentry.logger.fmt`User ${user} updated profile picture`, { |
| 100 | + * userId: 'user-123', |
| 101 | + * imageSize: '2.5MB', |
| 102 | + * timestamp: Date.now() |
| 103 | + * }); |
| 104 | + * ``` |
| 105 | + */ |
| 106 | +export function info(message: ParameterizedString, attributes?: Log['attributes']): void { |
| 107 | + captureLog('info', message, attributes); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * @summary Capture a log with the `warn` level. Requires `_experiments.enableLogs` to be enabled. |
| 112 | + * |
| 113 | + * @param message - The message to log. |
| 114 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., { browser: 'Chrome', version: '91.0' }. |
| 115 | + * |
| 116 | + * @example |
| 117 | + * |
| 118 | + * ``` |
| 119 | + * Sentry.logger.warn('Browser compatibility issue detected', { |
| 120 | + * browser: 'Safari', |
| 121 | + * version: '14.0', |
| 122 | + * feature: 'WebRTC', |
| 123 | + * fallback: 'enabled' |
| 124 | + * }); |
| 125 | + * ``` |
| 126 | + * |
| 127 | + * @example With template strings |
| 128 | + * |
| 129 | + * ``` |
| 130 | + * Sentry.logger.warn(Sentry.logger.fmt`API endpoint ${endpoint} is deprecated`, { |
| 131 | + * recommendedEndpoint: '/api/v2/users', |
| 132 | + * sunsetDate: '2024-12-31', |
| 133 | + * clientVersion: '1.2.3' |
| 134 | + * }); |
| 135 | + * ``` |
| 136 | + */ |
| 137 | +export function warn(message: ParameterizedString, attributes?: Log['attributes']): void { |
| 138 | + captureLog('warn', message, attributes); |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * @summary Capture a log with the `error` level. Requires `_experiments.enableLogs` to be enabled. |
| 143 | + * |
| 144 | + * @param message - The message to log. |
| 145 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., { error: 'NetworkError', url: '/api/data' }. |
| 146 | + * |
| 147 | + * @example |
| 148 | + * |
| 149 | + * ``` |
| 150 | + * Sentry.logger.error('Failed to load user data', { |
| 151 | + * error: 'NetworkError', |
| 152 | + * url: '/api/users/123', |
| 153 | + * statusCode: 500, |
| 154 | + * retryCount: 3 |
| 155 | + * }); |
| 156 | + * ``` |
| 157 | + * |
| 158 | + * @example With template strings |
| 159 | + * |
| 160 | + * ``` |
| 161 | + * Sentry.logger.error(Sentry.logger.fmt`Payment processing failed for order ${orderId}`, { |
| 162 | + * error: 'InsufficientFunds', |
| 163 | + * amount: 100.00, |
| 164 | + * currency: 'USD', |
| 165 | + * userId: 'user-456' |
| 166 | + * }); |
| 167 | + * ``` |
| 168 | + */ |
| 169 | +export function error(message: ParameterizedString, attributes?: Log['attributes']): void { |
| 170 | + captureLog('error', message, attributes); |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * @summary Capture a log with the `fatal` level. Requires `_experiments.enableLogs` to be enabled. |
| 175 | + * |
| 176 | + * @param message - The message to log. |
| 177 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., { appState: 'corrupted', sessionId: 'abc-123' }. |
| 178 | + * |
| 179 | + * @example |
| 180 | + * |
| 181 | + * ``` |
| 182 | + * Sentry.logger.fatal('Application state corrupted', { |
| 183 | + * lastKnownState: 'authenticated', |
| 184 | + * sessionId: 'session-123', |
| 185 | + * timestamp: Date.now(), |
| 186 | + * recoveryAttempted: true |
| 187 | + * }); |
| 188 | + * ``` |
| 189 | + * |
| 190 | + * @example With template strings |
| 191 | + * |
| 192 | + * ``` |
| 193 | + * Sentry.logger.fatal(Sentry.logger.fmt`Critical system failure in ${service}`, { |
| 194 | + * service: 'payment-processor', |
| 195 | + * errorCode: 'CRITICAL_FAILURE', |
| 196 | + * affectedUsers: 150, |
| 197 | + * timestamp: Date.now() |
| 198 | + * }); |
| 199 | + * ``` |
| 200 | + */ |
| 201 | +export function fatal(message: ParameterizedString, attributes?: Log['attributes']): void { |
| 202 | + captureLog('fatal', message, attributes); |
| 203 | +} |
| 204 | + |
| 205 | +export { fmt } from '@sentry/core'; |
0 commit comments