logging

package
v0.34.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

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

View Source
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

func (ll Format) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler.

func (*Format) UnmarshalText

func (ll *Format) UnmarshalText(text []byte) error

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) Filter

func (ll Level) Filter() level.Option

Filter returns a go-kit logging filter from the level.

func (Level) MarshalText

func (ll Level) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler.

func (*Level) UnmarshalText

func (ll *Level) UnmarshalText(text []byte) error

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.

func (*Logger) Log

func (c *Logger) Log(kvps ...interface{}) error

Log implements log.Logger.

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

func LoggerSink(c *Logger) *Sink

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL