|
| 1 | +import type { LogSeverityLevel, Log, Client } from '@sentry/core'; |
| 2 | +import { getClient, _INTERNAL_captureLog, _INTERNAL_flushLogsBuffer } from '@sentry/core'; |
| 3 | + |
| 4 | +import { WINDOW } from './helpers'; |
| 5 | + |
| 6 | +/** |
| 7 | + * TODO: Make this configurable |
| 8 | + */ |
| 9 | +const DEFAULT_FLUSH_INTERVAL = 5000; |
| 10 | + |
| 11 | +let timeout: ReturnType<typeof setTimeout> | undefined; |
| 12 | + |
| 13 | +/** |
| 14 | + * This is a global timeout that is used to flush the logs buffer. |
| 15 | + * It is used to ensure that logs are flushed even if the client is not flushed. |
| 16 | + */ |
| 17 | +function startFlushTimeout(client: Client): void { |
| 18 | + if (timeout) { |
| 19 | + clearTimeout(timeout); |
| 20 | + } |
| 21 | + |
| 22 | + timeout = setTimeout(() => { |
| 23 | + _INTERNAL_flushLogsBuffer(client); |
| 24 | + }, DEFAULT_FLUSH_INTERVAL); |
| 25 | +} |
| 26 | + |
| 27 | +let isClientListenerAdded = false; |
| 28 | +/** |
| 29 | + * This is a function that is used to add a flush listener to the client. |
| 30 | + * It is used to ensure that the logger buffer is flushed when the client is flushed. |
| 31 | + */ |
| 32 | +function addFlushingListeners(client: Client): void { |
| 33 | + if (isClientListenerAdded || !client.getOptions()._experiments?.enableLogs) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + isClientListenerAdded = true; |
| 38 | + |
| 39 | + if (WINDOW.document) { |
| 40 | + WINDOW.document.addEventListener('visibilitychange', () => { |
| 41 | + if (WINDOW.document.visibilityState === 'hidden') { |
| 42 | + _INTERNAL_flushLogsBuffer(client); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + client.on('flush', () => { |
| 48 | + _INTERNAL_flushLogsBuffer(client); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Capture a log with the given level. |
| 54 | + * |
| 55 | + * @param level - The level of the log. |
| 56 | + * @param message - The message to log. |
| 57 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 58 | + * @param severityNumber - The severity number of the log. |
| 59 | + */ |
| 60 | +function captureLog( |
| 61 | + level: LogSeverityLevel, |
| 62 | + message: string, |
| 63 | + attributes?: Log['attributes'], |
| 64 | + severityNumber?: Log['severityNumber'], |
| 65 | +): void { |
| 66 | + const client = getClient(); |
| 67 | + if (client) { |
| 68 | + addFlushingListeners(client); |
| 69 | + |
| 70 | + startFlushTimeout(client); |
| 71 | + } |
| 72 | + |
| 73 | + _INTERNAL_captureLog({ level, message, attributes, severityNumber }, client, undefined); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * @summary Capture a log with the `trace` level. Requires `_experiments.enableLogs` to be enabled. |
| 78 | + * |
| 79 | + * @param message - The message to log. |
| 80 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 81 | + * |
| 82 | + * @example |
| 83 | + * |
| 84 | + * ``` |
| 85 | + * Sentry.logger.trace('Hello world', { userId: 100 }); |
| 86 | + * ``` |
| 87 | + */ |
| 88 | +export function trace(message: string, attributes?: Log['attributes']): void { |
| 89 | + captureLog('trace', message, attributes); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * @summary Capture a log with the `debug` level. Requires `_experiments.enableLogs` to be enabled. |
| 94 | + * |
| 95 | + * @param message - The message to log. |
| 96 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 97 | + * |
| 98 | + * @example |
| 99 | + * |
| 100 | + * ``` |
| 101 | + * Sentry.logger.debug('Hello world', { userId: 100 }); |
| 102 | + * ``` |
| 103 | + */ |
| 104 | +export function debug(message: string, attributes?: Log['attributes']): void { |
| 105 | + captureLog('debug', message, attributes); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * @summary Capture a log with the `info` level. Requires `_experiments.enableLogs` to be enabled. |
| 110 | + * |
| 111 | + * @param message - The message to log. |
| 112 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 113 | + * |
| 114 | + * @example |
| 115 | + * |
| 116 | + * ``` |
| 117 | + * Sentry.logger.info('Hello world', { userId: 100 }); |
| 118 | + * ``` |
| 119 | + */ |
| 120 | +export function info(message: string, attributes?: Log['attributes']): void { |
| 121 | + captureLog('info', message, attributes); |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * @summary Capture a log with the `warn` level. Requires `_experiments.enableLogs` to be enabled. |
| 126 | + * |
| 127 | + * @param message - The message to log. |
| 128 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 129 | + * |
| 130 | + * @example |
| 131 | + * |
| 132 | + * ``` |
| 133 | + * Sentry.logger.warn('Hello world', { userId: 100 }); |
| 134 | + * ``` |
| 135 | + */ |
| 136 | +export function warn(message: string, attributes?: Log['attributes']): void { |
| 137 | + captureLog('warn', message, attributes); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @summary Capture a log with the `error` level. Requires `_experiments.enableLogs` to be enabled. |
| 142 | + * |
| 143 | + * @param message - The message to log. |
| 144 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 145 | + * |
| 146 | + * @example |
| 147 | + * |
| 148 | + * ``` |
| 149 | + * Sentry.logger.error('Hello world', { userId: 100 }); |
| 150 | + * ``` |
| 151 | + */ |
| 152 | +export function error(message: string, attributes?: Log['attributes']): void { |
| 153 | + captureLog('error', message, attributes); |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * @summary Capture a log with the `fatal` level. Requires `_experiments.enableLogs` to be enabled. |
| 158 | + * |
| 159 | + * @param message - The message to log. |
| 160 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 161 | + * |
| 162 | + * @example |
| 163 | + * |
| 164 | + * ``` |
| 165 | + * Sentry.logger.fatal('Hello world', { userId: 100 }); |
| 166 | + * ``` |
| 167 | + */ |
| 168 | +export function fatal(message: string, attributes?: Log['attributes']): void { |
| 169 | + captureLog('fatal', message, attributes); |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * @summary Capture a log with the `critical` level. Requires `_experiments.enableLogs` to be enabled. |
| 174 | + * |
| 175 | + * @param message - The message to log. |
| 176 | + * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. |
| 177 | + * |
| 178 | + * @example |
| 179 | + * |
| 180 | + * ``` |
| 181 | + * Sentry.logger.critical('Hello world', { userId: 100 }); |
| 182 | + * ``` |
| 183 | + */ |
| 184 | +export function critical(message: string, attributes?: Log['attributes']): void { |
| 185 | + captureLog('critical', message, attributes); |
| 186 | +} |
0 commit comments