A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://godoc.org/github.com/go-kit/log/level below:

level package - github.com/go-kit/log/level - Go Packages

Package level implements leveled logging on top of Go kit's log package. To use the level package, create a logger as per normal in your func main, and wrap it with level.NewFilter.

var logger log.Logger
logger = log.NewLogfmtLogger(os.Stderr)
logger = level.NewFilter(logger, level.AllowInfo()) // <--
logger = log.With(logger, "ts", log.DefaultTimestampUTC)

It's also possible to configure log level from a string. For instance from a flag, environment variable or configuration file.

fs := flag.NewFlagSet("myprogram")
lvl := fs.String("log", "info", "debug, info, warn, error")

var logger log.Logger
logger = log.NewLogfmtLogger(os.Stderr)
logger = level.NewFilter(logger, level.Allow(level.ParseDefault(*lvl, level.InfoValue()))) // <--
logger = log.With(logger, "ts", log.DefaultTimestampUTC)

Then, at the callsites, use one of the level.Debug, Info, Warn, or Error helper methods to emit leveled log events.

logger.Log("foo", "bar") // as normal, no level
level.Debug(logger).Log("request_id", reqID, "trace_data", trace.Get())
if value > 100 {
    level.Error(logger).Log("value", value)
}

NewFilter allows precise control over what happens when a log event is emitted without a level key, or if a squelched level is used. Check the Option functions for details.

package main

import (
	"os"

	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
)

func main() {
	logger := log.NewLogfmtLogger(os.Stdout)
	level.Debug(logger).Log("msg", "this message is at the debug level")
	level.Info(logger).Log("msg", "this message is at the info level")
	level.Warn(logger).Log("msg", "this message is at the warn level")
	level.Error(logger).Log("msg", "this message is at the error level")

}
Output:

level=debug msg="this message is at the debug level"
level=info msg="this message is at the info level"
level=warn msg="this message is at the warn level"
level=error msg="this message is at the error level"
package main

import (
	"errors"
	"os"

	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
)

func main() {
	// Set up logger with level filter.
	logger := log.NewLogfmtLogger(os.Stdout)
	logger = level.NewFilter(logger, level.AllowInfo())
	logger = log.With(logger, "caller", log.DefaultCaller)

	// Use level helpers to log at different levels.
	level.Error(logger).Log("err", errors.New("bad data"))
	level.Info(logger).Log("event", "data saved")
	level.Debug(logger).Log("next item", 17) // filtered

}
Output:

level=error caller=example_test.go:33 err="bad data"
level=info caller=example_test.go:34 event="data saved"
package main

import (
	"errors"
	"flag"
	"os"

	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
)

func main() {
	fs := flag.NewFlagSet("example", flag.ExitOnError)
	lvl := fs.String("log-level", "", `"debug", "info", "warn" or "error"`)
	fs.Parse([]string{"-log-level", "info"})

	// Set up logger with level filter.
	logger := log.NewLogfmtLogger(os.Stdout)
	logger = level.NewFilter(logger, level.Allow(level.ParseDefault(*lvl, level.DebugValue())))
	logger = log.With(logger, "caller", log.DefaultCaller)

	// Use level helpers to log at different levels.
	level.Error(logger).Log("err", errors.New("bad data"))
	level.Info(logger).Log("event", "data saved")
	level.Debug(logger).Log("next item", 17) // filtered

}
Output:

level=error caller=example_test.go:53 err="bad data"
level=info caller=example_test.go:54 event="data saved"

This section is empty.

ErrInvalidLevelString is returned whenever an invalid string is passed to Parse.

Debug returns a logger that includes a Key/DebugValue pair.

Error returns a logger that includes a Key/ErrorValue pair.

Info returns a logger that includes a Key/InfoValue pair.

Key returns the unique key added to log events by the loggers in this package.

NewFilter wraps next and implements level filtering. See the commentary on the Option functions for a detailed description of how to configure levels. If no options are provided, all leveled log events created with Debug, Info, Warn or Error helper methods are squelched and non-leveled log events are passed to next unmodified.

NewInjector wraps next and returns a logger that adds a Key/level pair to the beginning of log events that don't already contain a level. In effect, this gives a default level to logs without a level.

Warn returns a logger that includes a Key/WarnValue pair.

type Option func(*logger)

Option sets a parameter for the leveled logger.

Allow the provided log level to pass.

AllowAll is an alias for AllowDebug.

AllowDebug allows error, warn, info and debug level log events to pass.

AllowError allows only error level log events to pass.

AllowInfo allows error, warn and info level log events to pass.

AllowNone allows no leveled log events to pass.

AllowWarn allows error and warn level log events to pass.

ErrNoLevel sets the error to return from Log when it squelches a log event with no level. By default, ErrNoLevel is nil; in this case the log event is squelched with no error.

ErrNotAllowed sets the error to return from Log when it squelches a log event disallowed by the configured Allow[Level] option. By default, ErrNotAllowed is nil; in this case the log event is squelched with no error.

SquelchNoLevel instructs Log to squelch log events with no level, so that they don't proceed through to the wrapped logger. If SquelchNoLevel is set to true and a log event is squelched in this way, the error value configured with ErrNoLevel is returned to the caller.

type Value interface {
	String() string
	
}

Value is the interface that each of the canonical level values implement. It contains unexported methods that prevent types from other packages from implementing it and guaranteeing that NewFilter can distinguish the levels defined in this package from all other values.

DebugValue returns the unique value added to log events by Debug.

ErrorValue returns the unique value added to log events by Error.

InfoValue returns the unique value added to log events by Info.

Parse a string to its corresponding level value. Valid strings are "debug", "info", "warn", and "error". Strings are normalized via strings.TrimSpace and strings.ToLower.

ParseDefault calls Parse and returns the default Value on error.

WarnValue returns the unique value added to log events by Warn.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4