googleapis/mcp-toolbox · error

unable to initialize logger: %w

Error message

unable to initialize logger: %w

What it means

Returned by the public `Setup` method in cmd/internal/options.go when `log.NewLogger` cannot construct the application logger from the configured logging format, log level, and output streams. Since Setup is invoked by every entrypoint (runInvoke, runMigrate, runServe, run), an invalid logger configuration prevents the binary from starting at all. The wrapped error from NewLogger names the invalid format or level value.

Source

Thrown at cmd/internal/options.go:98

	return func(o *ToolboxOptions) {
		o.IOStreams.Out = out
		o.IOStreams.ErrOut = err
	}
}

// Setup create logger and telemetry instrumentations.
func (opts *ToolboxOptions) Setup(ctx context.Context) (context.Context, func(context.Context) error, error) {
	// If stdio, set logger's out stream (usually DEBUG and INFO logs) to
	// errStream
	loggerOut := opts.IOStreams.Out
	if opts.Cfg.Stdio {
		loggerOut = opts.IOStreams.ErrOut
	}

	// Handle logger separately from config
	logger, err := log.NewLogger(opts.Cfg.LoggingFormat.String(), opts.Cfg.LogLevel.String(), loggerOut, opts.IOStreams.ErrOut)
	if err != nil {
		return ctx, nil, fmt.Errorf("unable to initialize logger: %w", err)
	}

	ctx = util.WithLogger(ctx, logger)
	opts.Logger = logger

	ctx = util.WithIgnoreUnknownTools(ctx, opts.Cfg.IgnoreUnknownTools)

	logger.InfoContext(ctx, fmt.Sprintf("Starting MCP Toolbox for Databases version %s", opts.Cfg.Version))

	// Set up OpenTelemetry
	otelShutdown, err := telemetry.SetupOTel(ctx, opts.Cfg.Version, opts.Cfg.TelemetryOTLP, opts.Cfg.TelemetryGCP, opts.Cfg.TelemetryGCPProject, opts.Cfg.TelemetryServiceName)
	if err != nil {
		errMsg := fmt.Errorf("error setting up OpenTelemetry: %w", err)
		logger.ErrorContext(ctx, errMsg.Error())
		return ctx, nil, errMsg
	}

	shutdownFunc := func(ctx context.Context) error {

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Read the wrapped error text to see which value was rejected; fix --logging-format or --log-level to a supported value (e.g. logging-format: "json", log-level: "debug").
  2. Check environment variables (LOGGING_FORMAT, LOG_LEVEL) that override flags; unset or correct them.
  3. Trim whitespace/quotes around the value in the config file and re-run.
  4. Run `./toolbox --help` (or check docs/en/reference) to list the exact accepted format and level strings.

Example fix

// before: invalid value in tools.yaml
logging:
  format: "structured"
  level: "verbose"
// after
logging:
  format: "json"
  level: "debug"
Defensive patterns

Strategy: validation

Validate before calling

validFormats := map[string]bool{"standard": true, "json": true}
validLevels := map[string]bool{"debug": true, "info": true, "warn": true, "error": true}
if !validFormats[strings.TrimSpace(os.Getenv("LOGGING_FORMAT"))] && os.Getenv("LOGGING_FORMAT") != "" {
    log.Fatalf("LOGGING_FORMAT=%q is not supported", os.Getenv("LOGGING_FORMAT"))
}
if !validLevels[strings.TrimSpace(os.Getenv("LOG_LEVEL"))] && os.Getenv("LOG_LEVEL") != "" {
    log.Fatalf("LOG_LEVEL=%q is not supported", os.Getenv("LOG_LEVEL"))
}

Try / catch

ctx, shutdown, err := opts.Setup(ctx)
if err != nil {
    if strings.Contains(err.Error(), "unable to initialize logger") {
        fmt.Fprintf(os.Stderr, "logger config invalid: %v — check --logging-format and --log-level values\n", err)
        os.Exit(2)
    }
    return err
}

Prevention

When it happens

Trigger: Passing an unsupported value via the --logging-format flag (or LOGGING_FORMAT env / config file), e.g. "structured" or "jsonl" instead of a supported value like "json" or "standard"; similarly an invalid --log-level such as "verbose" or "trace" instead of debug/info/warn/error.

Common situations: Typo in a YAML/JSON config file's logging section after editing; CI pipeline exporting a custom LOGGING_FORMAT env var; upgrading the toolbox and using a log level/format string from a different tool's convention; quoting mistakes leaving stray whitespace or quotes in the config value.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/1162c2658ec21d1f. Report an issue: GitHub.