log

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: MIT Imports: 14 Imported by: 0

README

Log

Documentation

Index

Constants

View Source
const LevelKey = "level"

LevelKey is logger level key.

Variables

View Source
var (
	// DefaultTimestamp is a Valuer that returns the current wallclock time,
	// respecting time zones, when bound.
	DefaultTimestamp = TimestampFormat(time.Now(), time.RFC3339Nano)

	// DefaultTimestampUTC is a Valuer that returns the current time in UTC
	// when bound.
	DefaultTimestampUTC = TimestampFormat(time.Now().UTC(), time.RFC3339Nano)

	// DefaultCaller is a Valuer that returns the file and line where the Log
	// method was invoked. It can only be used with log.With.
	DefaultCaller = Caller(2)
)
View Source
var (
	// DefaultLogger is default logger.
	DefaultLogger = NewLogger()
)
View Source
var ErrMissingValue = errors.New("(MISSING)")

ErrMissingValue is appended to keyvals slices with odd length to substitute the missing value.

Functions

func Caller

func Caller(depth int) string

Caller returns a Valuer that returns a file and line from a specified depth in the callstack. Users will probably want to use DefaultCaller.

func Debug

func Debug(a ...interface{})

func Debugf

func Debugf(format string, a ...interface{})

func Error

func Error(a ...interface{})

func Errorf

func Errorf(format string, a ...interface{})

func Fatal

func Fatal(a ...interface{})

func Fatalf

func Fatalf(format string, a ...interface{})

func Info

func Info(a ...interface{})

func Infof

func Infof(format string, a ...interface{})

func Merge

func Merge(dst map[string]interface{}, k, v interface{})

func NewContext

func NewContext(ctx context.Context, l Logger) context.Context

func TimestampFormat

func TimestampFormat(t time.Time, layout string) string

TimestampFormat returns a timestamp Valuer with a custom time format. It invokes the t function to get the time to format; unless you are doing something tricky, pass time.Now. The layout string is passed to Time.Format.

Most users will want to use DefaultTimestamp or DefaultTimestampUTC, which are TimestampFormats that use the RFC3339Nano format.

func Warn

func Warn(a ...interface{})

func Warnf

func Warnf(format string, a ...interface{})

Types

type Helper

type Helper struct {
	Logger
	// contains filtered or unexported fields
}

func NewHelper

func NewHelper(log Logger) *Helper

func (*Helper) Debug

func (h *Helper) Debug(a ...interface{})

func (*Helper) Debugf

func (h *Helper) Debugf(format string, a ...interface{})

func (*Helper) Debugw

func (h *Helper) Debugw(kv ...interface{})

func (*Helper) Error

func (h *Helper) Error(a ...interface{})

func (*Helper) Errorf

func (h *Helper) Errorf(format string, a ...interface{})

func (*Helper) Errorw

func (h *Helper) Errorw(kv ...interface{})

func (*Helper) Fatal

func (h *Helper) Fatal(a ...interface{})

func (*Helper) Fatalf

func (h *Helper) Fatalf(format string, a ...interface{})

func (*Helper) Fatalw

func (h *Helper) Fatalw(kv ...interface{})

func (*Helper) Info

func (h *Helper) Info(a ...interface{})

func (*Helper) Infof

func (h *Helper) Infof(format string, a ...interface{})

func (*Helper) Infow

func (h *Helper) Infow(kv ...interface{})

func (*Helper) WithError

func (h *Helper) WithError(err error) *Helper

func (*Helper) WithFields

func (h *Helper) WithFields(fields map[string]interface{}) *Helper

type Level

type Level int8

Level is a logger level.

const (
	// LevelDebug is logger debug level.
	LevelDebug Level = iota
	// LevelInfo is logger info level.
	LevelInfo
	// LevelWarn is logger warn level.
	LevelWarn
	// LevelError is logger error level.
	LevelError
	// LevelFatal is logger fatal level.
	LevelFatal
)

func ParseLevel

func ParseLevel(s string) Level

ParseLevel parses a level string into a logger Level value.

func (Level) Enabled

func (l Level) Enabled(lv Level) bool

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	// Log writes a log entry
	Log(level Level, kvs ...interface{})
	// Fields set fields to always be logged
	Fields(fields map[string]interface{}) Logger
}

Logger is a logger interface.

func FromContext

func FromContext(ctx context.Context) Logger

func NewJSONLogger

func NewJSONLogger(opts ...Option) Logger

NewJSONLogger returns a Logger that encodes key and val to the Writer as a single JSON object. Each log event produces no more than one call to w.Write. The passed Writer must be safe for concurrent use by multiple goroutines if the returned Logger will be used concurrently.

func NewLogger

func NewLogger(opts ...Option) Logger

type Option

type Option func(*Options)

func WithCallerSkipCount

func WithCallerSkipCount(c int) Option

WithCallerSkipCount set frame count to skip

func WithLevel

func WithLevel(level Level) Option

WithLevel set default level for the logger

func WithOutput

func WithOutput(out io.Writer) Option

WithOutput set default output writer for the logger

type Options

type Options struct {
	// The logging level the logger should log at. default is `InfoLevel`
	Level Level
	// fields to always be logged
	Fields map[string]interface{}
	// It's common to set this to a file, or leave it default which is `os.Stderr`
	Out io.Writer
	// Caller skip frame count for file:line info
	CallerSkipCount int
	// Alternative options
	Context ctx.Context
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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