Effect CommunityEC
Effect Community3y ago
15 replies
Cosimo Matteini

Weird Behaviors with Custom Loggers and Errors

Hi everybody!

I've been playing with custom loggers and errors and found some weird behaviors.
This is the code I'm running: https://github.com/devmatteini/effect-playground/blob/f3bf57d9ff4d90977398f224701f583a005ca35e/src/custom-logger.ts

If I run the code without specifying the NAME environment variable with npx tsx src/custom-logger.ts I get the following output:

timestamp=2023-10-24T21:28:15.135Z level=INFO fiber=#1 message="DepsLive init"
timestamp=2023-10-24T21:28:15.140Z level=DEBUG fiber=#1 message="Fiber terminated with a non handled error" cause="(Missing data at NAME: \"Expected NAME to exist in the process context\")"
[APP_ERROR] (Missing data at NAME: "Expected NAME to exist in the process context")


The first "problem" I noticed is that the first two logs are not using my custom logger. Is it supposed to work like this?
Also, about the second log, since I'm using Effect.runPromiseExit I wasn't expecting a Fiber terminated with a non handled error. Am I missing something?

After some tests, I found out that providing DepsLive and Logger with two different Effect.provide calls resolves both “problems:

const result = await F.pipe(
    greet,
    Effect.tap((message) => Effect.logInfo(message)),
    Effect.asUnit,
-   Effect.provide(Layer.merge(DepsLive, Logger.replace(Logger.defaultLogger, customLogger))),
+   Effect.provide(DepsLive),
+   Effect.provide(Logger.replace(Logger.defaultLogger, customLogger)),
    Logger.withMinimumLogLevel(LogLevel.Debug),
    Effect.runPromiseExit,
)

and the console output:

[INFO] DepsLive init
[APP_ERROR] (Missing data at NAME: "Expected NAME to exist in the process context")
Was this page helpful?