|
8 | 8 | import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
|
9 | 9 | import * as loggerModule from '../../../src/utils-hoist/logger';
|
10 | 10 | import { Scope } from '../../../src';
|
| 11 | +import type { Log } from '../../../src/types-hoist/log'; |
11 | 12 |
|
12 | 13 | const PUBLIC_DSN = 'https://username@domain/123';
|
13 | 14 |
|
@@ -187,4 +188,94 @@ describe('_INTERNAL_captureLog', () => {
|
187 | 188 | _INTERNAL_flushLogsBuffer(client);
|
188 | 189 | expect(mockSendEnvelope).not.toHaveBeenCalled();
|
189 | 190 | });
|
| 191 | + |
| 192 | + it('processes logs through beforeSendLog when provided', () => { |
| 193 | + const beforeSendLog = vi.fn().mockImplementation(log => ({ |
| 194 | + ...log, |
| 195 | + message: `Modified: ${log.message}`, |
| 196 | + attributes: { ...log.attributes, processed: true }, |
| 197 | + })); |
| 198 | + |
| 199 | + const options = getDefaultTestClientOptions({ |
| 200 | + dsn: PUBLIC_DSN, |
| 201 | + _experiments: { enableLogs: true, beforeSendLog }, |
| 202 | + }); |
| 203 | + const client = new TestClient(options); |
| 204 | + |
| 205 | + _INTERNAL_captureLog( |
| 206 | + { |
| 207 | + level: 'info', |
| 208 | + message: 'original message', |
| 209 | + attributes: { original: true }, |
| 210 | + }, |
| 211 | + client, |
| 212 | + undefined, |
| 213 | + ); |
| 214 | + |
| 215 | + expect(beforeSendLog).toHaveBeenCalledWith({ |
| 216 | + level: 'info', |
| 217 | + message: 'original message', |
| 218 | + attributes: { original: true }, |
| 219 | + }); |
| 220 | + |
| 221 | + const logBuffer = _INTERNAL_getLogBuffer(client); |
| 222 | + expect(logBuffer).toBeDefined(); |
| 223 | + expect(logBuffer?.[0]).toEqual( |
| 224 | + expect.objectContaining({ |
| 225 | + body: { |
| 226 | + stringValue: 'Modified: original message', |
| 227 | + }, |
| 228 | + attributes: expect.arrayContaining([ |
| 229 | + expect.objectContaining({ key: 'processed', value: { boolValue: true } }), |
| 230 | + expect.objectContaining({ key: 'original', value: { boolValue: true } }), |
| 231 | + ]), |
| 232 | + }), |
| 233 | + ); |
| 234 | + }); |
| 235 | + |
| 236 | + it('drops logs when beforeSendLog returns null', () => { |
| 237 | + const beforeSendLog = vi.fn().mockReturnValue(null); |
| 238 | + const recordDroppedEventSpy = vi.spyOn(TestClient.prototype, 'recordDroppedEvent'); |
| 239 | + const loggerWarnSpy = vi.spyOn(loggerModule.logger, 'warn').mockImplementation(() => undefined); |
| 240 | + |
| 241 | + const options = getDefaultTestClientOptions({ |
| 242 | + dsn: PUBLIC_DSN, |
| 243 | + _experiments: { enableLogs: true, beforeSendLog }, |
| 244 | + }); |
| 245 | + const client = new TestClient(options); |
| 246 | + |
| 247 | + _INTERNAL_captureLog( |
| 248 | + { |
| 249 | + level: 'info', |
| 250 | + message: 'test message', |
| 251 | + }, |
| 252 | + client, |
| 253 | + undefined, |
| 254 | + ); |
| 255 | + |
| 256 | + expect(beforeSendLog).toHaveBeenCalled(); |
| 257 | + expect(recordDroppedEventSpy).toHaveBeenCalledWith('before_send', 'log_item', 1); |
| 258 | + expect(loggerWarnSpy).toHaveBeenCalledWith('beforeSendLog returned null, log will not be captured.'); |
| 259 | + expect(_INTERNAL_getLogBuffer(client)).toBeUndefined(); |
| 260 | + |
| 261 | + recordDroppedEventSpy.mockRestore(); |
| 262 | + loggerWarnSpy.mockRestore(); |
| 263 | + }); |
| 264 | + |
| 265 | + it('emits beforeCaptureLog and afterCaptureLog events', () => { |
| 266 | + const beforeCaptureLogSpy = vi.spyOn(TestClient.prototype, 'emit'); |
| 267 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } }); |
| 268 | + const client = new TestClient(options); |
| 269 | + |
| 270 | + const log: Log = { |
| 271 | + level: 'info', |
| 272 | + message: 'test message', |
| 273 | + }; |
| 274 | + |
| 275 | + _INTERNAL_captureLog(log, client, undefined); |
| 276 | + |
| 277 | + expect(beforeCaptureLogSpy).toHaveBeenCalledWith('beforeCaptureLog', log); |
| 278 | + expect(beforeCaptureLogSpy).toHaveBeenCalledWith('afterCaptureLog', log); |
| 279 | + beforeCaptureLogSpy.mockRestore(); |
| 280 | + }); |
190 | 281 | });
|
0 commit comments