Skip to content
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

Add Effect.whenLogLevel #4342

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

indietyp
Copy link

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

This is the result from the following Discord discussion: https://discord.com/channels/795981131316985866/1332442902629978132, it implements a new function Effect.whenLogLevel, for which the specified effect is only executed whenever the log level is enabled.

I am unsure if the return type here is okay, currently it permits the effect to return a value, and wrap the return value in Option.Option. The other possibility would be to simply say the return must be (like for Effect.tap) to be void. I could imagine scenarios in which it could be helpful to get the return value, but also feel like that it could be abused in a sense, 95% of usecases would likely just yield* without considering the return value (but one could for example use the return value to determine if other diagnostics should be generated, etc.)

Related

@indietyp indietyp requested a review from mikearnaldi as a code owner January 25, 2025 21:08
Copy link

changeset-bot bot commented Jan 25, 2025

🦋 Changeset detected

Latest commit: cc68a42

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 35 packages
Name Type
effect Patch
@effect/cli Patch
@effect/cluster-browser Patch
@effect/cluster-node Patch
@effect/cluster-workflow Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/printer-ansi Patch
@effect/printer Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-kysely Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql Patch
@effect/typeclass Patch
@effect/vitest Patch
@effect/ai Patch
@effect/ai-openai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gcanti gcanti requested a review from tim-smart January 30, 2025 12:08
<A, E, R>(effect: Effect.Effect<A, E, R>, level: LogLevel.LogLevel) => Effect.Effect<Option.Option<A>, E, R>
>(2, (effect, level) => {
return core.withFiberRuntime((fiberState) => {
const currentLogLevel = FiberRef.currentMinimumLogLevel.pipe(fiberState.getFiberRef)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the .pipe here

@@ -948,6 +948,20 @@ export const logAnnotations: Effect.Effect<HashMap.HashMap<string, unknown>> = c
core.currentLogAnnotations
)

/** @internal */
export const whenLogLevel = dual<
(level: LogLevel.LogLevel) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should support passing a string literal too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Waiting on Author
Development

Successfully merging this pull request may close these issues.

2 participants