Debugging SQL Command Logging in D1Client with Monkey-Patching

For debugging purposes, I've monkey-patched the D1Client to log SQL commands whenever they are executed. The code below worked, however it's not respecting the log level set at the entry point:
Logger.minimumLogLevel(LogLevel.None)

I'm guessing it's because of the runFrok at: E.logInfo(query, args).pipe(E.runFork)?

Do I need to change my program to use a managed runtime? or is there an easier way to fix it?

const D1Live = Layer.unwrapEffect(
  Effect.gen(function* () {
    const env = yield* WorkerEnv
    const db = env.getBinding<D1Database>("DB")

    const prepare: D1Database["prepare"] = (query) =>
      new Proxy(db.prepare(query), {
        get: (target, prop, receiver) => {
          if (prop !== "bind") return Reflect.get(target, prop, receiver)

          const bind: D1PreparedStatement["bind"] = (...args) => {
            E.logInfo("🔍 ___QUERY___", query, args).pipe(E.runFork)
            return target.bind(...args)
          }
          return bind
        },
      })

    const proxy = new Proxy(db, {
      get: (target, prop, receiver) =>
        prop === "prepare" ? prepare : Reflect.get(target, prop, receiver),
    })

    return D1Client.layer({
      db: Config.succeed(proxy),
    })
  }),
)
Was this page helpful?