{"record":{"id":"5eda62d7561f6002","repo":"cloudflare/cloudflared","slug":"error-setting-up-logger","errorCode":null,"errorMessage":"error setting up logger","messagePattern":"error setting up logger","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/tunnel/subcommands.go","lineNumber":278,"sourceCode":"  For example, to create a tunnel named 'my-tunnel' run:\n\n  $ cloudflared tunnel create my-tunnel`,\n\t\tFlags:              []cli.Flag{outputFormatFlag, credentialsFileFlagCLIOnly, createSecretFlag},\n\t\tCustomHelpTemplate: commandHelpTemplate(),\n\t}\n}\n\n// generateTunnelSecret as an array of 32 bytes using secure random number generator\nfunc generateTunnelSecret() ([]byte, error) {\n\trandomBytes := make([]byte, 32)\n\t_, err := rand.Read(randomBytes)\n\treturn randomBytes, err\n}\n\nfunc createCommand(c *cli.Context) error {\n\tsc, err := newSubcommandContext(c)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"error setting up logger\")\n\t}\n\n\tif c.NArg() != 1 {\n\t\treturn cliutil.UsageError(`\"cloudflared tunnel create\" requires exactly 1 argument, the name of tunnel to create.`)\n\t}\n\tname := c.Args().First()\n\n\twarningChecker := updater.StartWarningCheck(c)\n\tdefer warningChecker.LogWarningIfAny(sc.log)\n\n\t_, err = sc.create(name, c.String(CredFileFlag), c.String(createSecretFlag.Name))\n\treturn errors.Wrap(err, \"failed to create tunnel\")\n}\n\nfunc tunnelFilePath(tunnelID uuid.UUID, directory string) (string, error) {\n\tfileName := fmt.Sprintf(\"%v.json\", tunnelID)\n\tfilePath := filepath.Clean(fmt.Sprintf(\"%s/%s\", directory, fileName))\n\treturn homedir.Expand(filePath)","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/tunnel/subcommands.go#L260-L296","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the --loglevel value is a valid level (e.g. debug, info, warn, error)","Ensure the --logfile path's directory exists and is writable by the current user","Remove or fix custom log-related flags and retry with defaults","Inspect the wrapped cause printed after this message to identify the exact init failure"],"exampleFix":"// before\ncloudflared tunnel create my-tunnel --loglevel verbose --logfile /nonexistent/x.log\n// after\ncloudflared tunnel create my-tunnel --loglevel info --logfile /tmp/x.log","handlingStrategy":"validation","validationCode":"for _, p := range []string{\"loglevel\", \"logfile\", \"log-directory\"} {\n\tif v := c.String(p); v != \"\" {\n\t\tif p == \"loglevel\" && !validLevels[strings.ToLower(v)] {\n\t\t\treturn fmt.Errorf(\"invalid --loglevel %q\", v)\n\t\t}\n\t\tif p != \"loglevel\" && dir(filepath.Dir(v)) == nil {\n\t\t\treturn fmt.Errorf(\"log path not writable: %s\", v)\n\t\t}\n\t}\n}","typeGuard":null,"tryCatchPattern":"sc, err := newSubcommandContext(c)\nif err != nil {\n\treturn fmt.Errorf(\"error setting up logger: %w\", err)\n}","preventionTips":["Validate --loglevel values against supported levels in scripts","Ensure log file directories exist and are writable before launch","Test commands in the same user/container context as production","Avoid mutating log flags in wrappers without testing"],"tags":["cloudflared","logging","cli","initialization"],"backgroundTag":"logger-init-failed","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}