Overriding a Custom Logger in Effect Typescript

Is it possible to override a custom logger?

I tried the following but both the customLogger and the testLogger are firing. https://effect.website/play#a635beb112dd

I am a little confused because I would expect it to either replace the logger or do nothing because a logging layer is already provided, but not to have both at the same time.

import { Effect, Layer, Logger, LogLevel, pipe } from "effect"

const customLogger = Logger.make(({ logLevel, message }) => {
  globalThis.console.log(`[${logLevel.label}] ${message}`)
})

const mockLogger = (log: (options: Logger.Logger.Options<unknown>) => void) =>
  pipe(
    Logger.make(log),
    (logger) => Logger.replace(customLogger, logger),
    Layer.merge(Logger.minimumLogLevel(LogLevel.All))
  )

const program = pipe(
  Effect.log("something"),
  Effect.provide(Logger.replace(Logger.defaultLogger, customLogger))
)

const test = () => {
  const logs: string[] = []
  const testLogger = mockLogger(({ message }) => logs.push(String(message)))
  return program.pipe(
    Effect.tap(() => {
      console.log({ logs })
    }),
    Effect.provide(testLogger)
  )
}

Effect.runFork(test())
Was this page helpful?