setup parser
This commit is contained in:
37
internal/common/xlog/slog.go
Normal file
37
internal/common/xlog/slog.go
Normal file
@ -0,0 +1,37 @@
|
||||
package xlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func LevelAsSLogLevel(level Level) slog.Level {
|
||||
switch level {
|
||||
case LevelDebug:
|
||||
return slog.LevelDebug
|
||||
case LevelInfo:
|
||||
return slog.LevelInfo
|
||||
case LevelWarn:
|
||||
return slog.LevelWarn
|
||||
case LevelError:
|
||||
return slog.LevelError
|
||||
default:
|
||||
panic("unsupported level " + level.String())
|
||||
}
|
||||
}
|
||||
|
||||
func StringerAttr(name string, value fmt.Stringer) slog.Attr {
|
||||
return slog.Any(name, StringerValue(value))
|
||||
}
|
||||
|
||||
func StringerValue(s fmt.Stringer) slog.LogValuer {
|
||||
return stringerValue{s}
|
||||
}
|
||||
|
||||
type stringerValue struct {
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
func (s stringerValue) LogValue() slog.Value {
|
||||
return slog.StringValue(s.String())
|
||||
}
|
||||
67
internal/common/xlog/xlog.go
Normal file
67
internal/common/xlog/xlog.go
Normal file
@ -0,0 +1,67 @@
|
||||
package xlog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Level uint8
|
||||
|
||||
const (
|
||||
LevelDebug Level = iota
|
||||
LevelInfo
|
||||
LevelWarn
|
||||
LevelError
|
||||
)
|
||||
|
||||
func (l *Level) UnmarshalText(data []byte) error {
|
||||
switch levelstr := string(bytes.ToLower(data)); levelstr {
|
||||
case "debug":
|
||||
*l = LevelDebug
|
||||
case "info":
|
||||
*l = LevelInfo
|
||||
case "warn":
|
||||
*l = LevelWarn
|
||||
case "error":
|
||||
*l = LevelError
|
||||
default:
|
||||
return errors.New("unsupported level " + levelstr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l Level) String() string {
|
||||
switch l {
|
||||
case LevelDebug:
|
||||
return "debug"
|
||||
case LevelInfo:
|
||||
return "info"
|
||||
case LevelWarn:
|
||||
return "warn"
|
||||
case LevelError:
|
||||
return "error"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
type Format uint8
|
||||
|
||||
const (
|
||||
FormatText Format = iota
|
||||
FormatJSON
|
||||
)
|
||||
|
||||
func (f *Format) UnmarshalText(data []byte) error {
|
||||
switch formatstr := string(bytes.ToLower(data)); formatstr {
|
||||
case "debug":
|
||||
*f = FormatText
|
||||
case "info":
|
||||
*f = FormatJSON
|
||||
default:
|
||||
return errors.New("unsupported format " + formatstr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user