Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger 空指针 #313

Open
lafrinte opened this issue Dec 13, 2023 · 0 comments
Open

logger 空指针 #313

lafrinte opened this issue Dec 13, 2023 · 0 comments

Comments

@lafrinte
Copy link

Describe the bug
配置文件读取失败或解析失败情况下,logger.Fatal 会 panic

To Reproduce
./ferry server -c <不存在的配置路径>

Expected behavior
正常提示配置文件不存在或配置文件解析失败

Screenshots

❯ ./ferry server -c /tmp/1
2023/12/13 17:48:00 starting api server
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10044c055]

goroutine 1 [running]:
go.uber.org/zap.(*SugaredLogger).log(0x0, 0x5, {0x0?, 0x0?}, {0xc0025bfd18?, 0x100011b05?, 0x10?}, {0x0, 0x0, 0x0})
	/Users/lafrinte/go/pkg/mod/go.uber.org/zap@v1.10.0/sugar.go:233 +0xf5
go.uber.org/zap.(*SugaredLogger).Fatal(...)
	/Users/lafrinte/go/pkg/mod/go.uber.org/zap@v1.10.0/sugar.go:128
ferry/pkg/logger.Fatal(...)
	/Users/lafrinte/Downloads/ferry/pkg/logger/logger.go:116
ferry/tools/config.ConfigSetup({0x7ff7bfeff172?, 0x1?})
	/Users/lafrinte/Downloads/ferry/tools/config/config.go:23 +0x11e
ferry/cmd/api.setup()
	/Users/lafrinte/Downloads/ferry/cmd/api/server.go:56 +0x25
ferry/cmd/api.glob..func1(0xc000782800?, {0x10108da10?, 0x4?, 0x10108da14?})
	/Users/lafrinte/Downloads/ferry/cmd/api/server.go:34 +0x14
github.com/spf13/cobra.(*Command).execute(0x1030964a0, {0xc00183a6e0, 0x2, 0x2})
	/Users/lafrinte/go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:835 +0x603
github.com/spf13/cobra.(*Command).ExecuteC(0x103096200)
	/Users/lafrinte/go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:950 +0x389
github.com/spf13/cobra.(*Command).Execute(...)
	/Users/lafrinte/go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:887
ferry/cmd.Execute()
	/Users/lafrinte/Downloads/ferry/cmd/cobra.go:39 +0x1a
main.main()
	/Users/lafrinte/Downloads/ferry/main.go:8 +0xf

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context

func ConfigSetup(path string) {
	viper.SetConfigFile(path)
	content, err := ioutil.ReadFile(path)
	if err != nil {
		logger.Fatal(fmt.Sprintf("Read config file fail: %s", err.Error()))
	}

	//Replace environment variables
	err = viper.ReadConfig(strings.NewReader(os.ExpandEnv(string(content))))
	if err != nil {
		logger.Fatal(fmt.Sprintf("Parse config file fail: %s", err.Error()))
	}

        ...

	// 日志配置
	logger.Init()
}

logger.Init()前,logger.log 是空指针,logger.Fatal会 panic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant