Effect CommunityEC
Effect Community•2mo ago•
3 replies
Emanuele Mastaglia

Accessing --log-level Option for File Logger in Effect CLI

How to access --log-level CLI option value for file logger filtering I have an Effect CLI application with a global command that has a --logfile option. When this option is provided, I want to write logs to a file instead of the console, and respect the --log-level option for filtering:
// Global command with --logfile option
const globalCommand = Command.make("cli", { logfile: Options.text("logfile").pipe(Options.optional) }, ...);

// Subcommand that needs to configure file logging
const subCommand = Command.make("subcommand", {}, () =>
  Effect.flatMap(globalCommand, (config) =>
    Effect.gen(function* () {
      let pipeline = myPipeline();
      
      if (Option.isSome(config.logfile)) {
        const fileLogger = yield* Logger.logfmtLogger.pipe(
          PlatformLogger.toFile(config.logfile.value)
        );
        
        // Need to apply --log-level here, but how?
        const currentLogLevel = yield* FiberRef.get(FiberRef.currentLogLevel);
        // This always returns INFO, even when --log-level=debug is passed
        
        pipeline = pipeline.pipe(
          Effect.provide(Logger.replace(Logger.defaultLogger, fileLogger))
        );
      }
      
      return yield* pipeline;
    })
  )
);

Example CLI invocation:
cli --log-level=debug --logfile=output.log subcommand


The problem is that FiberRef.get(FiberRef.currentLogLevel) returns INFO even when --log-level=debug is passed. I suspect this is because I'm reading it too early, before the CLI runtime applies the log level layer. How can I access the --log-level value configured by the Effect CLI to pass it to Logger.minimumLogLevel() for the file logger?

Any helps would be apreciated 😅 😅
Was this page helpful?