package logging
import (
"encoding"
"fmt"
"github.com/go-kit/log/level"
"github.com/grafana/agent/pkg/river"
)
type SinkOptions struct {
Level Level `river:"level,attr,optional"`
Format Format `river:"format,attr,optional"`
IncludeTimestamps bool
}
var DefaultSinkOptions = SinkOptions{
Level: LevelDefault,
Format: FormatDefault,
IncludeTimestamps: true,
}
var _ river.Unmarshaler = (*SinkOptions)(nil)
func (o *SinkOptions) UnmarshalRiver(f func(interface{}) error) error {
*o = DefaultSinkOptions
type options SinkOptions
return f((*options)(o))
}
type Level string
const (
LevelDebug Level = "debug"
LevelInfo Level = "info"
LevelWarn Level = "warn"
LevelError Level = "error"
LevelDefault = LevelInfo
)
var (
_ encoding.TextMarshaler = LevelDefault
_ encoding.TextUnmarshaler = (*Level)(nil)
)
func (ll Level) MarshalText() (text []byte, err error) {
return []byte(ll), nil
}
func (ll *Level) UnmarshalText(text []byte) error {
switch Level(text) {
case "":
*ll = LevelDefault
case LevelDebug, LevelInfo, LevelWarn, LevelError:
*ll = Level(text)
default:
return fmt.Errorf("unrecognized log level %q", string(text))
}
return nil
}
func (ll Level) Filter() level.Option {
switch ll {
case LevelDebug:
return level.AllowDebug()
case LevelInfo:
return level.AllowInfo()
case LevelWarn:
return level.AllowWarn()
case LevelError:
return level.AllowError()
default:
return level.AllowAll()
}
}
type Format string
const (
FormatLogfmt Format = "logfmt"
FormatJSON Format = "json"
FormatDefault = FormatLogfmt
)
var (
_ encoding.TextMarshaler = FormatDefault
_ encoding.TextUnmarshaler = (*Format)(nil)
)
func (ll Format) MarshalText() (text []byte, err error) {
return []byte(ll), nil
}
func (ll *Format) UnmarshalText(text []byte) error {
switch Format(text) {
case "":
*ll = FormatDefault
case FormatLogfmt, FormatJSON:
*ll = Format(text)
default:
return fmt.Errorf("unrecognized log format %q", string(text))
}
return nil
}