logger

package
v3.10.13 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: Apache-2.0 Imports: 21 Imported by: 15

Documentation

Overview

Package log provides a log interface

Index

Constants

View Source
const (
	// TimeKey is the key used by the built-in handlers for the time
	// when the log method is called. The associated Value is a [time.Time].
	TimeKey = "ts"
	// PidKey is the key used by the built-in handlers for the pid
	// when the log level is lower than InfoLevel.
	PidKey = "pid"
	// MessageKey is the key used by the built-in handlers for the
	// message of the log call. The associated value is a string.
	MessageKey = "msg"
	// NanoTimeFieldFormat indicates the format of timestamp decoded
	// from a float value (time in seconds and nanoseconds).
	NanoTimeFieldFormat = "2006-01-02 15:04:05.999999999"
)

Keys for "built-in" attributes.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(template string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(template string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(template string, args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(template string, args ...interface{})

func Init

func Init(opts ...Option) error

func Log

func Log(level Level, v ...interface{})

func Logf

func Logf(level Level, format string, v ...interface{})

func NewContext

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

func NewContextWithKey added in v3.10.6

func NewContextWithKey(ctx context.Context, key any, l Logger) context.Context

func String

func String() string

func Trace

func Trace(args ...interface{})

func Tracef

func Tracef(template string, args ...interface{})

func V

func V(lvl Level, log Logger) bool

Returns true if the given level is at or lower the current logger level

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(template string, args ...interface{})

Types

type Buffer added in v3.10.6

type Buffer []byte

buffer adapted from go/src/fmt/print.go

func NewBuffer added in v3.10.6

func NewBuffer() *Buffer

func (*Buffer) Bytes added in v3.10.6

func (b *Buffer) Bytes() []byte

func (*Buffer) Free added in v3.10.6

func (b *Buffer) Free()

func (*Buffer) Reset added in v3.10.6

func (b *Buffer) Reset()

func (*Buffer) String added in v3.10.6

func (b *Buffer) String() string

func (*Buffer) Write added in v3.10.6

func (b *Buffer) Write(p []byte) (int, error)

func (*Buffer) WriteByte added in v3.10.6

func (b *Buffer) WriteByte(c byte) error

func (*Buffer) WritePosInt added in v3.10.6

func (b *Buffer) WritePosInt(i int)

func (*Buffer) WritePosIntWidth added in v3.10.6

func (b *Buffer) WritePosIntWidth(i, width int)

WritePosIntWidth writes non-negative integer i to the buffer, padded on the left by zeroes to the given width. Use a width of 0 to omit padding.

func (*Buffer) WriteString added in v3.10.6

func (b *Buffer) WriteString(s string)

type Format added in v3.10.6

type Format int8
const (
	TextFormat Format = iota
	JsonFormat
)

func (Format) String added in v3.10.6

func (f Format) String() string

type Handler added in v3.10.6

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

func NewHandler added in v3.10.6

func NewHandler(opts *Options) (h *Handler)

NewJSONHandler creates a Handler that writes to w, using the default options.

func (*Handler) Enabled added in v3.10.6

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*Handler) Handle added in v3.10.6

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle formats its argument Record as a JSON object on a single line.

func (*Handler) WithAttrs added in v3.10.6

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

With returns a new Handler whose attributes consists of h's attributes followed by attrs.

func (*Handler) WithGroup added in v3.10.6

func (h *Handler) WithGroup(name string) slog.Handler

type HandlerOptions added in v3.10.6

type HandlerOptions struct {

	// Ignore records with levels below Level.Level().
	// The default is InfoLevel.
	Level slog.Leveler

	// ReplaceAttr can be used to change the default keys of the built-in
	// attributes, convert types (for example, to replace a `time.Time` with the
	// integer seconds since the Unix epoch), sanitize personal information, or
	// remove attributes from the output.
	ReplaceAttr func(a slog.Attr) slog.Attr
}

HandlerOptions are options for a TextHandler or Handler. A zero HandlerOptions consists entirely of default values.

func (HandlerOptions) NewHandler added in v3.10.6

func (opts HandlerOptions) NewHandler(w io.Writer, json bool) *Handler

NewJSONHandler creates a Handler with the given options that writes to w.

type Helper

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

Helper for logger

func NewHelper

func NewHelper(log Logger) *Helper

func SetDefault added in v3.10.6

func SetDefault(l Logger) *Helper

SetDefault sets the default Logger and returns default.

func (*Helper) Debug

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

func (*Helper) Debugf

func (h *Helper) Debugf(template string, args ...interface{})

func (*Helper) Error

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

func (*Helper) Errorf

func (h *Helper) Errorf(template string, args ...interface{})

func (*Helper) Fatal

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

func (*Helper) Fatalf

func (h *Helper) Fatalf(template string, args ...interface{})

func (*Helper) Info

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

func (*Helper) Infof

func (h *Helper) Infof(template string, args ...interface{})

func (*Helper) Trace

func (h *Helper) Trace(args ...interface{})

func (*Helper) Tracef

func (h *Helper) Tracef(template string, args ...interface{})

func (*Helper) Warn

func (h *Helper) Warn(args ...interface{})

func (*Helper) Warnf

func (h *Helper) Warnf(template string, args ...interface{})

func (*Helper) WithError

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

func (*Helper) WithFields

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

WithFields write logger fields

type Level

type Level int8
const (
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel Level = iota - 2
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// InfoLevel is the default logging priority.
	// General operational entries about what's going on inside the application.
	InfoLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	ErrorLevel
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity.
	FatalLevel
)

func GetLevel

func GetLevel(levelStr string) (Level, error)

GetLevel converts a level string into a logger Level value. returns an error if the input string does not match known values.

func (Level) Enabled

func (l Level) Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	// Init initializes options
	Init(options ...Option) error
	// The Logger options
	Options() Options
	// Fields set fields to always be logged
	Fields(fields map[string]interface{}) Logger
	// Log writes a log entry
	Log(level Level, v ...interface{})
	// Logf writes a formatted log entry
	Logf(level Level, format string, v ...interface{})
	// String returns the name of logger
	String() string
}

Logger is a generic logging interface

var DefaultLogger Logger

func Fields

func Fields(fields map[string]interface{}) Logger

func FromContext

func FromContext(ctx context.Context) (Logger, bool)

func FromContextWithKey added in v3.10.6

func FromContextWithKey(ctx context.Context, key any) (Logger, bool)

func GetDefault added in v3.10.6

func GetDefault() Logger

GetDefault returns the default Logger.

func NewLogger

func NewLogger(opts ...Option) Logger

NewLogger builds a new logger based on options

type Option

type Option func(*Options)

func WithCallerSkipCount

func WithCallerSkipCount(c int) Option

WithCallerSkipCount set frame count to skip

func WithContext added in v3.10.6

func WithContext(k, v interface{}) Option

WithContext set default context for the logger

func WithFields

func WithFields(fields map[string]interface{}) Option

WithFields set default fields for the logger

func WithFormat added in v3.10.6

func WithFormat(f Format) Option

WithFormat set default output format for the logger

func WithHandler added in v3.10.6

func WithHandler(h slog.Handler) Option

WithHandler set default handler for the logger

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 context.Context
	// Format log print format options
	Format Format
	// A Handler handles log records produced by a Logger..
	//
	// A typical handler may print log records to standard error,
	// or write them to a file or database, or perhaps augment them
	// with additional attributes and pass them on to another handler.
	//
	// Any of the Handler's methods may be called concurrently with itself
	// or with other methods. It is the responsibility of the Handler to
	// manage this concurrency.
	Handler slog.Handler
}

Jump to

Keyboard shortcuts

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