Пример #1
0
func TestLoad(t *testing.T) {
	conf, err := config.Load("../../default-config.json")
	assert.Nil(t, err)
	assert.Equal(t, conf.MaxLogSizeMb, 100)
	assert.Equal(t, conf.MaxLogAgeDays, 28)
	assert.Equal(t, conf.LogPath, "/var/log/docker-volume-vsphere.log")
}
Пример #2
0
// init log with passed logLevel (and get config from configFile if it's present)
// returns True if using defaults,  False if using config file
func logInit(logLevel *string, logFile *string, configFile *string) bool {
	level, err := log.ParseLevel(*logLevel)
	if err != nil {
		panic(fmt.Sprintf("Failed to parse log level: %v", err))
	}

	usingConfigDefaults := false
	c, err := config.Load(*configFile)
	if err != nil {
		if os.IsNotExist(err) {
			usingConfigDefaults = true // no .conf file, so using defaults
			c = config.Config{}
			config.SetDefaults(&c)
		} else {
			panic(fmt.Sprintf("Failed to load config file %s: %v",
				*configFile, err))
		}
	}

	path := c.LogPath
	if logFile != nil {
		path = *logFile
	}
	log.SetOutput(&lumberjack.Logger{
		Filename: path,
		MaxSize:  c.MaxLogSizeMb,  // megabytes
		MaxAge:   c.MaxLogAgeDays, // days
	})

	log.SetFormatter(new(VmwareFormatter))
	log.SetLevel(level)

	if usingConfigDefaults {
		log.Info("No config file found. Using defaults.")
	}
	return usingConfigDefaults
}