log

package
v0.0.0-...-52e7125 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: AGPL-3.0 Imports: 13 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Logger is a shared go-kit logger.
	// TODO: Change all components to take a non-global logger via their constructors.
	// Prefer accepting a non-global logger as an argument.
	Logger = log.NewNopLogger()
)

Functions

func CheckFatal

func CheckFatal(location string, err error)

CheckFatal prints an error and exits with error code 1 if err is non-nil

func Flush

func Flush() error

Flush forces the buffered logger, if configured, to flush to the underlying writer This is typically only called when the application is shutting down.

func InitLogger

func InitLogger(logFormat string, logLevel dslog.Level, buffered bool, rateLimitedCfg RateLimitedLoggerCfg) log.Logger

InitLogger initialises the global gokit logger (util_log.Logger) and returns that logger.

Example

Check that debug lines are correctly filtered out and that rate limit is satisfied.

package main

import (
	"os"
	"time"

	gokitlog "github.com/go-kit/log"
	"github.com/go-kit/log/level"
	"github.com/grafana/dskit/server"
	"github.com/prometheus/client_golang/prometheus"

	"github.com/grafana/mimir/pkg/util/log"
)

func main() {
	// Kludge a couple of things so we can do tests repeatably.
	saveStderr := os.Stderr
	os.Stderr = os.Stdout
	saveTimestamp := gokitlog.DefaultTimestampUTC
	gokitlog.DefaultTimestampUTC = gokitlog.TimestampFormat(
		func() time.Time { return time.Unix(0, 0).UTC() },
		time.RFC3339Nano,
	)

	cfg := server.Config{}
	_ = cfg.LogLevel.Set("info")
	rateLimitedCfg := log.RateLimitedLoggerCfg{
		Enabled:       true,
		LogsPerSecond: 1,
		LogsBurstSize: 4,
		Registry:      prometheus.NewPedanticRegistry(),
	}
	cfg.Log = log.InitLogger(cfg.LogFormat, cfg.LogLevel, false, rateLimitedCfg)

	for i := 0; i < 1000; i++ {
		level.Info(log.Logger).Log("msg", "log.Logger", "test", i+1)
		level.Debug(log.Logger).Log("msg", "log.Logger", "test", i+1)
		level.Info(cfg.Log).Log("msg", "cfg.Log", "test", i+1)
		level.Debug(cfg.Log).Log("msg", "log.Logger", "test", i+1)
	}

	os.Stderr = saveStderr
	gokitlog.DefaultTimestampUTC = saveTimestamp
}
Output:

ts=1970-01-01T00:00:00Z caller=log_test.go:41 level=info msg=log.Logger test=1
ts=1970-01-01T00:00:00Z caller=log_test.go:43 level=info msg=cfg.Log test=1
ts=1970-01-01T00:00:00Z caller=log_test.go:41 level=info msg=log.Logger test=2
ts=1970-01-01T00:00:00Z caller=log_test.go:43 level=info msg=cfg.Log test=2

func WarnExperimentalUse

func WarnExperimentalUse(feature string)

WarnExperimentalUse logs a warning and increments the experimental features metric.

func WithContext

func WithContext(ctx context.Context, l log.Logger) log.Logger

WithContext returns a Logger that has information about the current user or users and trace in its details.

e.g.

log = util.WithContext(ctx, log)
# level=error user=user-1|user-2 traceID=123abc msg="Could not chunk chunks" err="an error"
level.Error(log).Log("msg", "Could not chunk chunks", "err", err)

func WithSourceIPs

func WithSourceIPs(sourceIPs string, l log.Logger) log.Logger

WithSourceIPs returns a Logger that has information about the source IPs in its details.

func WithTraceID

func WithTraceID(traceID string, l log.Logger) log.Logger

WithTraceID returns a Logger that has information about the traceID in its details.

func WithUserID

func WithUserID(userID string, l log.Logger) log.Logger

WithUserID returns a Logger that has information about the current user in its details.

func WithUserIDs

func WithUserIDs(userIDs []string, l log.Logger) log.Logger

WithUserIDs returns a Logger that has information about the current user or users (separated by "|") in its details.

Types

type RateLimitedLogger

type RateLimitedLogger struct {
	// contains filtered or unexported fields
}

RateLimitedLogger implements log.Logger and permits only a single log invocation every interval.

func NewRateLimitedLogger

func NewRateLimitedLogger(interval time.Duration, delegate log.Logger, now func() time.Time) RateLimitedLogger

func (RateLimitedLogger) Log

func (r RateLimitedLogger) Log(keyvals ...interface{}) error

type RateLimitedLoggerCfg

type RateLimitedLoggerCfg struct {
	Enabled       bool
	LogsPerSecond float64
	LogsBurstSize int
	Registry      prometheus.Registerer
}

type SampledError

type SampledError struct {
	// contains filtered or unexported fields
}

func (SampledError) Error

func (s SampledError) Error() string

func (SampledError) ShouldLog

func (s SampledError) ShouldLog(_ context.Context) (bool, string)

ShouldLog is called by common logging module.

func (SampledError) Unwrap

func (s SampledError) Unwrap() error

type Sampler

type Sampler struct {
	// contains filtered or unexported fields
}

func NewSampler

func NewSampler(freq int64) *Sampler

func (*Sampler) Sample

func (s *Sampler) Sample() bool

func (*Sampler) WrapError

func (s *Sampler) WrapError(err error) error

Jump to

Keyboard shortcuts

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