cloudflare/cloudflared · error

error setting up logger

Error message

error setting up logger

What it means

`cloudflared tunnel create` first builds a subcommandContext via newSubcommandContext(c), which initializes the command logger and API client. If that fails, the error is wrapped as 'error setting up logger' — so the message is slightly misleading: the failure is in subcommand context initialization (logging setup, client bootstrap), not only logging.

Source

Thrown at cmd/cloudflared/tunnel/subcommands.go:278

  For example, to create a tunnel named 'my-tunnel' run:

  $ cloudflared tunnel create my-tunnel`,
		Flags:              []cli.Flag{outputFormatFlag, credentialsFileFlagCLIOnly, createSecretFlag},
		CustomHelpTemplate: commandHelpTemplate(),
	}
}

// generateTunnelSecret as an array of 32 bytes using secure random number generator
func generateTunnelSecret() ([]byte, error) {
	randomBytes := make([]byte, 32)
	_, err := rand.Read(randomBytes)
	return randomBytes, err
}

func createCommand(c *cli.Context) error {
	sc, err := newSubcommandContext(c)
	if err != nil {
		return errors.Wrap(err, "error setting up logger")
	}

	if c.NArg() != 1 {
		return cliutil.UsageError(`"cloudflared tunnel create" requires exactly 1 argument, the name of tunnel to create.`)
	}
	name := c.Args().First()

	warningChecker := updater.StartWarningCheck(c)
	defer warningChecker.LogWarningIfAny(sc.log)

	_, err = sc.create(name, c.String(CredFileFlag), c.String(createSecretFlag.Name))
	return errors.Wrap(err, "failed to create tunnel")
}

func tunnelFilePath(tunnelID uuid.UUID, directory string) (string, error) {
	fileName := fmt.Sprintf("%v.json", tunnelID)
	filePath := filepath.Clean(fmt.Sprintf("%s/%s", directory, fileName))
	return homedir.Expand(filePath)

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Check the --loglevel value is a valid level (e.g. debug, info, warn, error)
  2. Ensure the --logfile path's directory exists and is writable by the current user
  3. Remove or fix custom log-related flags and retry with defaults
  4. Inspect the wrapped cause printed after this message to identify the exact init failure

Example fix

// before
cloudflared tunnel create my-tunnel --loglevel verbose --logfile /nonexistent/x.log
// after
cloudflared tunnel create my-tunnel --loglevel info --logfile /tmp/x.log
Defensive patterns

Strategy: validation

Validate before calling

for _, p := range []string{"loglevel", "logfile", "log-directory"} {
	if v := c.String(p); v != "" {
		if p == "loglevel" && !validLevels[strings.ToLower(v)] {
			return fmt.Errorf("invalid --loglevel %q", v)
		}
		if p != "loglevel" && dir(filepath.Dir(v)) == nil {
			return fmt.Errorf("log path not writable: %s", v)
		}
	}
}

Try / catch

sc, err := newSubcommandContext(c)
if err != nil {
	return fmt.Errorf("error setting up logger: %w", err)
}

Prevention

When it happens

Trigger: Running `cloudflared tunnel create` when the underlying logger configuration fails — typically a bad --loglevel value, an unwritable --logfile path, or a failure resolving the logging transport during newSubcommandContext.

Common situations: Read-only or nonexistent output directory given to --logfile; invalid log level string on the command line; container/CI images with no writable log location; mis-ordered shell quoting around log flags.

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/5eda62d7561f6002. Report an issue: GitHub.