Skip to content

Commit

Permalink
level: implement pflag.Value interface (#8)
Browse files Browse the repository at this point in the history
Implement pflag.Value interface for Level so it can be used as a CLI
flag.

Signed-off-by: Luiz Aoqui <luizaoqui@loopholelabs.io>
  • Loading branch information
lgfa29 authored Oct 16, 2024
1 parent ef47fb8 commit f631b02
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

package types

import "net"
import (
"errors"
"net"
"strings"
)

var (
ErrInvalidLogLevel = errors.New("invalid log level")
)

type Level int

Expand All @@ -15,6 +23,50 @@ const (
TraceLevel
)

func (l Level) String() string {
switch l {
case FatalLevel:
return "FATAL"
case ErrorLevel:
return "ERROR"
case WarnLevel:
return "WARN"
case InfoLevel:
return "INFO"
case DebugLevel:
return "DEBUG"
case TraceLevel:
return "TRACE"
default:
return "invalid"
}
}

func (l Level) Type() string {
return "log level"
}

func (l *Level) Set(v string) error {
switch strings.ToLower(v) {
case "fatal":
*l = FatalLevel
case "error":
*l = ErrorLevel
case "warn":
*l = WarnLevel
case "info":
*l = InfoLevel
case "debug":
*l = DebugLevel
case "trace":
*l = TraceLevel
default:
return ErrInvalidLogLevel
}

return nil
}

const (
TimestampKey = "time"
ErrorKey = "error"
Expand Down

0 comments on commit f631b02

Please sign in to comment.