logger.Levle: implement flag.Value

This commit is contained in:
Christian Schwarz 2017-12-30 13:52:51 +01:00
parent 01e0519b7b
commit 6f68c98c16

View File

@ -1,6 +1,7 @@
package logger
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
@ -23,6 +24,29 @@ func (l *Level) UnmarshalJSON(input []byte) (err error) {
return err
}
// implement flag.Value
// implement github.com/spf13/pflag.Value
func (l *Level) Set(s string) error {
newl, err := ParseLevel(s)
if err != nil {
return err
}
*l = newl
return nil
}
// implement github.com/spf13/pflag.Value
func (l *Level) Type() string {
var buf bytes.Buffer
for i, l := range AllLevels {
fmt.Fprintf(&buf, "%s", l)
if i != len(AllLevels)-1 {
fmt.Fprintf(&buf, "|")
}
}
return fmt.Sprintf("(%s)", buf.String())
}
const (
Debug Level = iota
Info