package fog import ( "flag" "fmt" "strconv" "strings" ) var levels = map[string]int8{ "info": 0, "debug": 1, "error": -1, // -1 signals via options to only log errors } var flagOpts options func BindFlags(_ *flag.FlagSet) { var lvl lvlFlag lvl.setFunc = func(v int8) { flagOpts.lvl = v } } type lvlFlag struct { setFunc func(int8) value string } // assert that our log level flag implements interface for binding to a command // line flag var _ flag.Value = &lvlFlag{} // Set checks if the flag value matches one of the string representations for a // logging level. If it is, it returns the corresponding int from the levels map. // Otherwise the value is converted into an integer. Passing an integer value // greater than 1 will allow you to use logging levels above debug, even though // that is the most verbose log level in GCP. func (f *lvlFlag) Set(v string) error { f.value = v lvl, ok := levels[strings.ToLower(v)] if ok { f.setFunc(lvl) return nil } intLvl, err := strconv.Atoi(v) if err != nil || intLvl < 0 { return fmt.Errorf("invalid log level '%s': %w", v, err) } f.setFunc(int8(toZapLevel(intLvl))) return nil } func (f *lvlFlag) String() string { return f.value }