Documentation ¶
Overview ¶
Example ¶
package main import ( "os" "github.com/go-kit/log/level" "github.com/grafana/agent/pkg/flow/logging" ) func main() { // Create a sink to send logs to. WriterSink supports options to customize // the logs sent to the sink. sink, err := logging.WriterSink(os.Stdout, logging.SinkOptions{ Level: logging.LevelDebug, Format: logging.FormatLogfmt, IncludeTimestamps: false, }) if err != nil { panic(err) } // Create a controller logger. A controller logger is a logger with no // component ID. controller := logging.New(sink) // Create two component loggers. The first component sends logs to the // controller, and the other sends logs to the first component. component1 := logging.New(logging.LoggerSink(controller), logging.WithComponentID("outer")) component2 := logging.New(logging.LoggerSink(component1), logging.WithComponentID("inner")) innerController := logging.New(logging.LoggerSink(component2)) // Log some log lines. level.Info(controller).Log("msg", "hello from the controller!") level.Info(component1).Log("msg", "hello from the outer component!") level.Info(component2).Log("msg", "hello from the inner component!") level.Info(innerController).Log("msg", "hello from the inner controller!") }
Output: level=info msg="hello from the controller!" component=outer level=info msg="hello from the outer component!" component=outer/inner level=info msg="hello from the inner component!" component=outer/inner level=info msg="hello from the inner controller!"
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultSinkOptions = SinkOptions{ Level: LevelDefault, Format: FormatDefault, IncludeTimestamps: true, }
DefaultSinkOptions holds defaults for creating a logging sink.
Functions ¶
This section is empty.
Types ¶
type Format ¶
type Format string
Format represents a text format to use when writing logs.
const ( FormatLogfmt Format = "logfmt" FormatJSON Format = "json" FormatDefault = FormatLogfmt )
Supported log formats.
func (Format) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Format) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Level ¶
type Level string
Level represents how verbose logging should be.
const ( LevelDebug Level = "debug" LevelInfo Level = "info" LevelWarn Level = "warn" LevelError Level = "error" LevelDefault = LevelInfo )
Supported log levels
func (Level) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Level) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a logger for Grafana Agent Flow components and controllers. It implements the log.Logger interface.
func New ¶
func New(sink *Sink, opts ...LoggerOption) *Logger
New creates a new Logger from the provided logging Sink.
type LoggerOption ¶ added in v0.33.0
type LoggerOption func(*Logger)
LoggerOption is passed to New to customize the constructed Logger.
func WithComponentID ¶ added in v0.33.0
func WithComponentID(id string) LoggerOption
WithComponentID provides a component ID to the Logger.
type Sink ¶ added in v0.33.0
type Sink struct {
// contains filtered or unexported fields
}
Sink is where a Controller logger will send log lines to.
func LoggerSink ¶ added in v0.33.0
LoggerSink forwards logs to the provided Logger. The component ID from the provided Logger will be propagated to any new Loggers created using this Sink. LoggerSink does not support being updated.
func WriterSink ¶ added in v0.33.0
func WriterSink(w io.Writer, o SinkOptions) (*Sink, error)
WriterSink forwards logs to the provided io.Writer. WriterSinks support being updated.
func (*Sink) Update ¶ added in v0.33.0
func (s *Sink) Update(o SinkOptions) error
Update reconfigures the options used for the Sink. Update will return an error if the options are invalid or if the Sink doesn't support being given SinkOptions.
type SinkOptions ¶ added in v0.33.0
type SinkOptions struct { Level Level `river:"level,attr,optional"` Format Format `river:"format,attr,optional"` // IncludeTimestamps disables timestamps on log lines. It is not exposed as a // river tag as it is only expected to be used during tests. IncludeTimestamps bool }
SinkOptions is a set of options used to construct and configure a logging sink.
func (*SinkOptions) UnmarshalRiver ¶ added in v0.33.0
func (o *SinkOptions) UnmarshalRiver(f func(interface{}) error) error
UnmarshalRiver implements river.Unmarshaler.