Documentation ¶
Index ¶
- Variables
- func CheckFatal(location string, err error)
- func Flush() error
- func InitLogger(logFormat string, logLevel dslog.Level, buffered bool, ...) log.Logger
- func SlogFromGoKit(logger log.Logger) *slog.Logger
- func WarnExperimentalUse(feature string)
- func WithContext(ctx context.Context, l log.Logger) log.Logger
- func WithSourceIPs(sourceIPs string, l log.Logger) log.Logger
- func WithTraceID(traceID string, l log.Logger) log.Logger
- func WithUserID(userID string, l log.Logger) log.Logger
- func WithUserIDs(userIDs []string, l log.Logger) log.Logger
- type RateLimitedLogger
- type RateLimitedLoggerCfg
- type SampledError
- type Sampler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 SlogFromGoKit ¶
SlogFromGoKit returns slog adapter for logger.
func WarnExperimentalUse ¶
func WarnExperimentalUse(feature string)
WarnExperimentalUse logs a warning and increments the experimental features metric.
func WithContext ¶
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 ¶
WithSourceIPs returns a Logger that has information about the source IPs in its details.
func WithTraceID ¶
WithTraceID returns a Logger that has information about the traceID in its details.
func WithUserID ¶
WithUserID returns a Logger that has information about the current user 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 (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