-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(effect): Add logging to Sentry.effectLayer #19656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+250
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { logger as sentryLogger } from '@sentry/core'; | ||
| import * as Logger from 'effect/Logger'; | ||
|
|
||
| /** | ||
| * Effect Logger that sends logs to Sentry. | ||
| */ | ||
| export const SentryEffectLogger = Logger.make(({ logLevel, message }) => { | ||
| let msg: string; | ||
| if (typeof message === 'string') { | ||
| msg = message; | ||
| } else if (Array.isArray(message) && message.length === 1) { | ||
| const firstElement = message[0]; | ||
| msg = typeof firstElement === 'string' ? firstElement : JSON.stringify(firstElement); | ||
| } else { | ||
| msg = JSON.stringify(message); | ||
| } | ||
|
|
||
| switch (logLevel._tag) { | ||
| case 'Fatal': | ||
| sentryLogger.fatal(msg); | ||
| break; | ||
| case 'Error': | ||
| sentryLogger.error(msg); | ||
| break; | ||
| case 'Warning': | ||
| sentryLogger.warn(msg); | ||
| break; | ||
| case 'Info': | ||
| sentryLogger.info(msg); | ||
| break; | ||
| case 'Debug': | ||
| sentryLogger.debug(msg); | ||
| break; | ||
| case 'Trace': | ||
| sentryLogger.trace(msg); | ||
| break; | ||
| case 'All': | ||
| case 'None': | ||
| break; | ||
| default: | ||
| logLevel satisfies never; | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { describe, expect, it } from '@effect/vitest'; | ||
| import * as sentryCore from '@sentry/core'; | ||
| import { Effect, Layer, Logger, LogLevel } from 'effect'; | ||
| import { afterEach, vi } from 'vitest'; | ||
| import { SentryEffectLogger } from '../src/logger'; | ||
|
|
||
| vi.mock('@sentry/core', async importOriginal => { | ||
| const original = await importOriginal<typeof sentryCore>(); | ||
| return { | ||
| ...original, | ||
| logger: { | ||
| ...original.logger, | ||
| error: vi.fn(), | ||
| warn: vi.fn(), | ||
| info: vi.fn(), | ||
| debug: vi.fn(), | ||
| trace: vi.fn(), | ||
| fatal: vi.fn(), | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| describe('SentryEffectLogger', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| const loggerLayer = Layer.mergeAll( | ||
| Logger.replace(Logger.defaultLogger, SentryEffectLogger), | ||
| Logger.minimumLogLevel(LogLevel.All), | ||
| ); | ||
|
|
||
| it.effect('forwards fatal logs to Sentry', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logFatal('This is a fatal message'); | ||
| expect(sentryCore.logger.fatal).toHaveBeenCalledWith('This is a fatal message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('forwards error logs to Sentry', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logError('This is an error message'); | ||
| expect(sentryCore.logger.error).toHaveBeenCalledWith('This is an error message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('forwards warning logs to Sentry', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logWarning('This is a warning message'); | ||
| expect(sentryCore.logger.warn).toHaveBeenCalledWith('This is a warning message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('forwards info logs to Sentry', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logInfo('This is an info message'); | ||
| expect(sentryCore.logger.info).toHaveBeenCalledWith('This is an info message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('forwards debug logs to Sentry', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logDebug('This is a debug message'); | ||
| expect(sentryCore.logger.debug).toHaveBeenCalledWith('This is a debug message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('forwards trace logs to Sentry', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logTrace('This is a trace message'); | ||
| expect(sentryCore.logger.trace).toHaveBeenCalledWith('This is a trace message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('handles object messages by stringifying', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logInfo({ key: 'value', nested: { foo: 'bar' } }); | ||
| expect(sentryCore.logger.info).toHaveBeenCalledWith('{"key":"value","nested":{"foo":"bar"}}'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('handles multiple log calls', () => | ||
| Effect.gen(function* () { | ||
| yield* Effect.logInfo('First message'); | ||
| yield* Effect.logInfo('Second message'); | ||
| yield* Effect.logWarning('Third message'); | ||
| expect(sentryCore.logger.info).toHaveBeenCalledTimes(2); | ||
| expect(sentryCore.logger.info).toHaveBeenNthCalledWith(1, 'First message'); | ||
| expect(sentryCore.logger.info).toHaveBeenNthCalledWith(2, 'Second message'); | ||
| expect(sentryCore.logger.warn).toHaveBeenCalledWith('Third message'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
|
|
||
| it.effect('works with Effect.tap for logging side effects', () => | ||
| Effect.gen(function* () { | ||
| const result = yield* Effect.succeed('data').pipe( | ||
| Effect.tap(data => Effect.logInfo(`Processing: ${data}`)), | ||
| Effect.map(d => d.toUpperCase()), | ||
| ); | ||
| expect(result).toBe('DATA'); | ||
| expect(sentryCore.logger.info).toHaveBeenCalledWith('Processing: data'); | ||
| }).pipe(Effect.provide(loggerLayer)), | ||
| ); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feat PR missing integration or E2E test
Low Severity
This is a
featPR that adds Effect log forwarding to Sentry, but it only includes vitest unit tests. Per the project review rules,featPRs are expected to include at least one integration or E2E test. The existing tests validate the logger and layer composition well at the unit level, but an integration or E2E test (e.g., indev-packages/e2e-tests/ordev-packages/node-integration-tests/) verifying that Effect logs actually arrive as Sentry log entries end-to-end would increase confidence.Additional Locations (1)
packages/effect/test/buildEffectLayer.test.ts#L64-L101Triggered by project rule: PR Review Guidelines for Cursor Bot