log

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: Apache-2.0 Imports: 14 Imported by: 1

README

Golang logging package support context.

Example:

package main

import (
	"context"

	"gitee.com/stn81/log"
)

func foo(ctx context.Context) {
	ctx = log.WithContext(ctx, "context", "foo")

	log.Info(ctx, "foo called")
}

func bar(ctx context.Context) {
	ctx = log.WithContext(ctx, "context", "bar")

	log.Info(ctx, "bar called")
}

func main() {
	ctx := log.WithContext(context.Background(), "module", "example")
	log.Info(ctx, "program started")
	foo(ctx)
	bar(ctx)
	log.Info(ctx, "program exited")
}

Output:

INFO    2017-04-09 10:30:16.648 [8875] module=[example] msg=[program started] fileline=[main.go:23]
INFO    2017-04-09 10:30:16.648 [8875] module=[example] context=[foo] msg=[foo called] fileline=[main.go:12]
INFO    2017-04-09 10:30:16.648 [8875] module=[example] context=[bar] msg=[bar called] fileline=[main.go:18]
INFO    2017-04-09 10:30:16.648 [8875] module=[example] msg=[program exited] fileline=[main.go:26]

Documentation

Index

Constants

View Source
const (
	OpenFlag = os.O_CREATE | os.O_APPEND | os.O_WRONLY | syscall.O_DSYNC
	OpenPerm = 0644
)
View Source
const MaxCachedBufferSize = 1 << 16 // 64KB

Variables

View Source
var ErrMissingValue = errors.New("(MISSING)")
View Source
var (
	PipeKVFormatter = &pipeKvFormatter{}
)
View Source
var RotateSignal = syscall.SIGUSR1

Functions

func Debug

func Debug(ctx context.Context, msg string, keyvals ...interface{})

func Enabled

func Enabled(level Level) bool

func Error

func Error(ctx context.Context, msg string, keyvals ...interface{})

func Fatal

func Fatal(ctx context.Context, msg string, keyvals ...interface{})

func GetStack

func GetStack(calldepth int) []byte

func Info

func Info(ctx context.Context, msg string, keyvals ...interface{})

func SetLevel

func SetLevel(level Level)

func SetLevelByName

func SetLevelByName(levelName string)

func SetLogger

func SetLogger(logger *Logger)

func Trace

func Trace(ctx context.Context, msg string, keyvals ...interface{})

func Warning

func Warning(ctx context.Context, msg string, keyvals ...interface{})

func WithContext

func WithContext(ctx context.Context, keyvals ...interface{}) context.Context

Types

type Appender

type Appender interface {
	Append(*Entry)
}

type Context

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

func (*Context) With

func (c *Context) With(keyvals ...interface{}) *Context

func (*Context) WithPrefix

func (c *Context) WithPrefix(keyvals ...interface{}) *Context

type Entry

type Entry struct {
	Tag     string
	Msg     string
	File    string
	Line    int
	Level   Level
	Time    time.Time
	KeyVals []interface{}
}

type FileAppender

type FileAppender struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewFileAppender

func NewFileAppender(levelMask Level, fileName string, formatter Formatter) (appender *FileAppender, err error)

func (*FileAppender) Append

func (appender *FileAppender) Append(entry *Entry)

func (*FileAppender) DisableLock

func (appender *FileAppender) DisableLock()

func (*FileAppender) Rotate

func (appender *FileAppender) Rotate()

type Formatter

type Formatter interface {
	Format(*Entry) (*bytes.Buffer, error)
}

type Level

type Level uint8
const (
	LevelTrace Level = 1 << iota
	LevelDebug
	LevelInfo
	LevelWarning
	LevelError
	LevelFatal
	LevelMask = 0xFF
)

func GetLevel

func GetLevel() Level

func (Level) String

func (level Level) String() string

type LevelName

type LevelName string

func (LevelName) ToLevel

func (levelName LevelName) ToLevel() Level

type Logger

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

func GetLogger

func GetLogger() *Logger

func NewLogger

func NewLogger(appenders ...Appender) *Logger

func (*Logger) Debug

func (logger *Logger) Debug(ctx context.Context, msg string, keyvals ...interface{})

func (*Logger) Enabled

func (logger *Logger) Enabled(level Level) bool

func (*Logger) Error

func (logger *Logger) Error(ctx context.Context, msg string, keyvals ...interface{})

func (*Logger) Fatal

func (logger *Logger) Fatal(ctx context.Context, msg string, keyvals ...interface{})

func (*Logger) GetLevel

func (logger *Logger) GetLevel() Level

func (*Logger) Info

func (logger *Logger) Info(ctx context.Context, msg string, keyvals ...interface{})

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level Level)

func (*Logger) Tag

func (logger *Logger) Tag(tag string) *TagLogger

func (*Logger) Trace

func (logger *Logger) Trace(ctx context.Context, msg string, keyvals ...interface{})

func (*Logger) Warning

func (logger *Logger) Warning(ctx context.Context, msg string, keyvals ...interface{})

type RotatableAppender

type RotatableAppender interface {
	Appender
	Rotate()
}

type StdAppender

type StdAppender struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewStdAppender

func NewStdAppender(formatter Formatter) *StdAppender

func (*StdAppender) Append

func (appender *StdAppender) Append(entry *Entry)

type TagLogger

type TagLogger struct {
	*Logger
	// contains filtered or unexported fields
}

func Tag

func Tag(tag string) *TagLogger

func (*TagLogger) Debug

func (logger *TagLogger) Debug(ctx context.Context, msg string, keyvals ...interface{})

func (*TagLogger) Error

func (logger *TagLogger) Error(ctx context.Context, msg string, keyvals ...interface{})

func (*TagLogger) Fatal

func (logger *TagLogger) Fatal(ctx context.Context, msg string, keyvals ...interface{})

func (*TagLogger) Info

func (logger *TagLogger) Info(ctx context.Context, msg string, keyvals ...interface{})

func (*TagLogger) Trace

func (logger *TagLogger) Trace(ctx context.Context, msg string, keyvals ...interface{})

func (*TagLogger) Warning

func (logger *TagLogger) Warning(ctx context.Context, msg string, keyvals ...interface{})

type Valuer

type Valuer func(context.Context) interface{}

A Valuer generates a log value. When passed to Context.With in a value element (odd indexes), it represents a dynamic value which is re-evaluated with each log event.

Jump to

Keyboard shortcuts

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