repo/common/logger.gopackage commonvar once sync.Oncevar Logger *logrus.Loggerfunc InitLogger() { once.Do(func() { Logger = logrus.New() Logger.Out = filename // file opened and *file assigned here for logging }) return Logger}回购/setup_test.gopackage mainfunc setUp() { common.InitLogger() fmt.Println(common.Logger) // prints some pointer related things which means logger is initialized}func TestMain(m *testing.M) { fmt.Println("Starting Test...") setUp() code := m.Run() common.APILog.Println("Finishing Main...") os.Exit(code)}回购/车辆/品牌/maruti/car/mycar_test.gopackage carfunc TestMyFunc(t *testing.T) { **t.Log(common.Logger) // When i run go test ./... this Prints nil , Why ?** // Doing some tests}由于这个 common.Logger.Println("some data") 到处都给出零指针错误根据我的理解,一旦 Logger 被初始化,它应该是可用的,因为它是一个全局变量。我肯定在这里遗漏了一些微妙的东西。请帮忙
- 1 回答
- 0 关注
- 282 浏览
添加回答
举报
0/150
提交
取消
