log

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 2 Imported by: 0

README

log

Log provides generic logger that is pluggable

How to Use

Logger interface contains commonly used logging usecases. Inject Logger into your object. Use adapters provided (logrus or zap)

// service.go
type service struct {
  logger log.Logger
}

func NewService(logger log.Logger) *service {
  return &service{logger: logger}
}

func (s *service) DoSomething() error {
  // do your thing
  if err != nil {
    s.logger.Error("some error happened")
    return err
  }
}

// main.go
import (
  "github.com/sirupsen/logrus"

	gogox_logrus "github.com/raymondwongso/gogox/log/logrus"
)

func main() {
  logrus := logrus.New()
  logrus.SetFormatter(&logrus.JSONFormatter{})
  logrus.SetLevel(logrus.ErrorLevel)

  logger := gogox_logrus.New(logrus, nil)

  service := service.NewService(logger)
}

Metadata

You can add metadata into your log entries, commonly used to store additional error info. Use [severity]w method.

s.logger.Errorw("some error happened", log.Metadata{"error": err.Error(), "user_id": 123})

// result
{"error":"some error happened","level":"error","msg":"some error happened","service":"api_logrus","time":"2023-02-03T11:32:54+07:00","user_id":123}

You can also add baseMetadata into your logger instance, commonly used to store global metadata for all your log entries.

logger := gogox_logrus.New(logrus, log.Metadata{"service": "api", "version": "1.2.1"})

s.logger.Errorw("some error happened", log.Metadata{"error": err.Error(), "user_id": 123})

// result
{"error":"some error come up","level":"error","msg":"some error come up","service":"api","time":"2023-02-03T11:35:25+07:00","user_id":1,"version":"1.2.1"}

Context

Use NewContext to inject your log metadata to context.

ctx := log.NewContext(context.Background(), log.Metadata{"user_id": 123})

Use MetadataFromContext to extract your log metadata from context

md := log.MetadataFromContext(ctx)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(parentCtx context.Context, md Metadata) context.Context

NewContext create new context that is injected with log.Metadata

Types

type LogLevel

type LogLevel int

LogLevel defines log level

const (
	TraceLevel LogLevel = iota
	DebugLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
	PanicLevel
)

type Logger

type Logger interface {
	Trace(msg string, args ...interface{})
	Tracew(msg string, md Metadata, args ...interface{})
	Debug(msg string, args ...interface{})
	Debugw(msg string, md Metadata, args ...interface{})
	Info(msg string, args ...interface{})
	Infow(msg string, md Metadata, args ...interface{})
	Warn(msg string, args ...interface{})
	Warnw(msg string, md Metadata, args ...interface{})
	Error(msg string, args ...interface{})
	Errorw(msg string, md Metadata, args ...interface{})
	Fatal(msg string, args ...interface{})
	Fatalw(msg string, md Metadata, args ...interface{})
	Panic(msg string, args ...interface{})
	Panicw(msg string, md Metadata, args ...interface{})

	Log(level LogLevel, msg string, args ...interface{})
	Logw(level LogLevel, msg string, md Metadata, args ...interface{})
}

Logger defines generic interface for all logger.

type Metadata

type Metadata map[string]interface{}

Metadata defines metadata for logger, which will be included whenever log is written.

func MergeMetadata

func MergeMetadata(md1, md2 Metadata) Metadata

MergeMetadata creates new metadata as a result from merging between md1 and md2.

func MetadataFromContext

func MetadataFromContext(ctx context.Context) Metadata

MetadataFromContext extract log.Metadata from context

type MetadataContextKeyType

type MetadataContextKeyType string
const (
	// MetadataContextKey is a custom context key for metadata
	MetadataContextKey MetadataContextKeyType = "metadata.context.key"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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