go-hclog: github.com/hashicorp/go-hclog Index | Files | Directories

package hclog

import "github.com/hashicorp/go-hclog"

Index

Package Files

context.go global.go intlogger.go logger.go nulllogger.go stacktrace.go stdlog.go writer.go

Constants

const TimeFormat = "2006-01-02T15:04:05.000Z0700"

TimeFormat to use for logging. This is a version of RFC3339 that contains contains millisecond precision

Variables

var (
    //DefaultOutput is used as the default log output.
    DefaultOutput io.Writer = os.Stderr

    // DefaultLevel is used as the default log level.
    DefaultLevel = Info
)
var (

    // DefaultOptions is used to create the Default logger. These are read
    // only when the Default logger is created, so set them as soon as the
    // process starts.
    DefaultOptions = &LoggerOptions{
        Level:  DefaultLevel,
        Output: DefaultOutput,
    }
)

func WithContext Uses

func WithContext(ctx context.Context, logger Logger, args ...interface{}) context.Context

WithContext inserts a logger into the context and is retrievable with FromContext. The optional args can be set with the same syntax as Logger.With to set fields on the inserted logger. This will not modify the logger argument in-place.

type CapturedStacktrace Uses

type CapturedStacktrace string

CapturedStacktrace represents a stacktrace captured by a previous call to log.Stacktrace. If passed to a logging function, the stacktrace will be appended.

func Stacktrace Uses

func Stacktrace() CapturedStacktrace

Stacktrace captures a stacktrace of the current goroutine and returns it to be passed to a logging function.

type Format Uses

type Format []interface{}

Format is a simple convience type for when formatting is required. When processing a value of this type, the logger automatically treats the first argument as a Printf formatting string and passes the rest as the values to be formatted. For example: L.Info(Fmt{"%d beans/day", beans}).

func Fmt Uses

func Fmt(str string, args ...interface{}) Format

Fmt returns a Format type. This is a convience function for creating a Format type.

type Level Uses

type Level int32

Level represents a log level.

const (
    // NoLevel is a special level used to indicate that no level has been
    // set and allow for a default to be used.
    NoLevel Level = 0

    // Trace is the most verbose level. Intended to be used for the tracing
    // of actions in code, such as function enters/exits, etc.
    Trace Level = 1

    // Debug information for programmer lowlevel analysis.
    Debug Level = 2

    // Info information about steady state operations.
    Info Level = 3

    // Warn information about rare but handled events.
    Warn Level = 4

    // Error information about unrecoverable events.
    Error Level = 5
)

func LevelFromString Uses

func LevelFromString(levelStr string) Level

LevelFromString returns a Level type for the named log level, or "NoLevel" if the level string is invalid. This facilitates setting the log level via config or environment variable by name in a predictable way.

type LevelWriter Uses

type LevelWriter interface {
    LevelWrite(level Level, p []byte) (n int, err error)
}

LevelWriter is the interface that wraps the LevelWrite method.

type LeveledWriter Uses

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

LeveledWriter writes all log messages to the standard writer, except for log levels that are defined in the overrides map.

func NewLeveledWriter Uses

func NewLeveledWriter(standard io.Writer, overrides map[Level]io.Writer) *LeveledWriter

NewLeveledWriter returns an initialized LeveledWriter.

standard will be used as the default writer for all log levels, except for log levels that are defined in the overrides map.

func (*LeveledWriter) LevelWrite Uses

func (lw *LeveledWriter) LevelWrite(level Level, p []byte) (int, error)

LevelWrite implements LevelWriter.

func (*LeveledWriter) Write Uses

func (lw *LeveledWriter) Write(p []byte) (int, error)

Write implements io.Writer.

type Logger Uses

type Logger interface {
    // Args are alternating key, val pairs
    // keys must be strings
    // vals can be any type, but display is implementation specific
    // Emit a message and key/value pairs at the TRACE level
    Trace(msg string, args ...interface{})

    // Emit a message and key/value pairs at the DEBUG level
    Debug(msg string, args ...interface{})

    // Emit a message and key/value pairs at the INFO level
    Info(msg string, args ...interface{})

    // Emit a message and key/value pairs at the WARN level
    Warn(msg string, args ...interface{})

    // Emit a message and key/value pairs at the ERROR level
    Error(msg string, args ...interface{})

    // Indicate if TRACE logs would be emitted. This and the other Is* guards
    // are used to elide expensive logging code based on the current level.
    IsTrace() bool

    // Indicate if DEBUG logs would be emitted. This and the other Is* guards
    IsDebug() bool

    // Indicate if INFO logs would be emitted. This and the other Is* guards
    IsInfo() bool

    // Indicate if WARN logs would be emitted. This and the other Is* guards
    IsWarn() bool

    // Indicate if ERROR logs would be emitted. This and the other Is* guards
    IsError() bool

    // Creates a sublogger that will always have the given key/value pairs
    With(args ...interface{}) Logger

    // Create a logger that will prepend the name string on the front of all messages.
    // If the logger already has a name, the new value will be appended to the current
    // name. That way, a major subsystem can use this to decorate all it's own logs
    // without losing context.
    Named(name string) Logger

    // Create a logger that will prepend the name string on the front of all messages.
    // This sets the name of the logger to the value directly, unlike Named which honor
    // the current name as well.
    ResetNamed(name string) Logger

    // Updates the level. This should affect all sub-loggers as well. If an
    // implementation cannot update the level on the fly, it should no-op.
    SetLevel(level Level)

    // Return a value that conforms to the stdlib log.Logger interface
    StandardLogger(opts *StandardLoggerOptions) *log.Logger

    // Return a value that conforms to io.Writer, which can be passed into log.SetOutput()
    StandardWriter(opts *StandardLoggerOptions) io.Writer
}

Logger describes the interface that must be implemeted by all loggers.

func Default Uses

func Default() Logger

Default returns a globally held logger. This can be a good starting place, and then you can use .With() and .Name() to create sub-loggers to be used in more specific contexts. The value of the Default logger can be set via SetDefault() or by changing the options in DefaultOptions.

This method is goroutine safe, returning a global from memory, but cause should be used if SetDefault() is called it random times in the program as that may result in race conditions and an unexpected Logger being returned.

func FromContext Uses

func FromContext(ctx context.Context) Logger

FromContext returns a logger from the context. This will return L() (the default logger) if no logger is found in the context. Therefore, this will never return a nil value.

func L Uses

func L() Logger

L is a short alias for Default().

func New Uses

func New(opts *LoggerOptions) Logger

New returns a configured logger.

func NewNullLogger Uses

func NewNullLogger() Logger

NewNullLogger instantiates a Logger for which all calls will succeed without doing anything. Useful for testing purposes.

func SetDefault Uses

func SetDefault(log Logger) Logger

SetDefault changes the logger to be returned by Default()and L() to the one given. This allows packages to use the default logger and have higher level packages change it to match the execution environment. It returns any old default if there is one.

NOTE: This is expected to be called early in the program to setup a default logger. As such, it does not attempt to make itself not racy with regard to the value of the default logger. Ergo if it is called in goroutines, you may experience race conditions with other goroutines retrieving the default logger. Basically, don't do that.

type LoggerOptions Uses

type LoggerOptions struct {
    // Name of the subsystem to prefix logs with
    Name string

    // The threshold for the logger. Anything less severe is supressed
    Level Level

    // Where to write the logs to. Defaults to os.Stderr if nil
    Output io.Writer

    // An optional mutex pointer in case Output is shared
    Mutex *sync.Mutex

    // Control if the output should be in JSON.
    JSONFormat bool

    // Include file and line information in each log line
    IncludeLocation bool

    // The time format to use instead of the default
    TimeFormat string
}

LoggerOptions can be used to configure a new logger.

type StandardLoggerOptions Uses

type StandardLoggerOptions struct {
    // Indicate that some minimal parsing should be done on strings to try
    // and detect their level and re-emit them.
    // This supports the strings like [ERROR], [ERR] [TRACE], [WARN], [INFO],
    // [DEBUG] and strip it off before reapplying it.
    InferLevels bool

    // ForceLevel is used to force all output from the standard logger to be at
    // the specified level. Similar to InferLevels, this will strip any level
    // prefix contained in the logged string before applying the forced level.
    // If set, this override InferLevels.
    ForceLevel Level
}

StandardLoggerOptions can be used to configure a new standard logger.

Directories

PathSynopsis
hclogvetVet is a simple checker for static errors in Go source code.

Package hclog imports 17 packages (graph) and is imported by 399 packages. Updated 2019-05-14. Refresh now. Tools for package owners.