README

LogC

A common and universal logging interface that every package in the simple frameworks org uses.

test status

Adapters to common logging libraries are provided so whatever logging library that you decide to use can be supported.


type Logger interface {
	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
	WithError(err error) Logger
}

Download it
go get -u github.com/simpleframeworks/logc
Logrus example

An outgoing adapter is used to send log messages. Here we use Logrus.


log = logc.NewLogrus(logrus.New())

log.Trace("some Trace log")
log.Debug("some Debug log")
log.Info("some Info log")
log.Warn("some Warn log")
log.Error("some Error log")


log.WithField("RequestID",1234).Trace("some Trace log")


log.WithFields(map[string]interface{}{
  "RequestID":  1234,
  "Name":       "SomeName",
}).Info("some Info log")


someError := errors.New("an error occurred")
log.WithError(someError).Error("some Error log")

Gorm adapter

An incoming adapter is used to collect log messages. Here we are collecting logs from Gorm


log = logc.NewLogrus(logrus.New())

log.Info("connecting to db - started")

db, err0 := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{
	Logger: logging.NewGormLogger(log),
})

log.Info("connecting to db - completed")

log.Info("running db auto migration - started")

db.AutoMigrate(&User{})

log.Info("running db auto migration - completed")

user := User{Name: "shmc", Age: 18, Birthday: time.Now()}
result := db.Create(&user)


Currently only logrus is supported but more packages are planned.

Documentation

Overview

Example (Logger)
logrusLog := logrus.New()
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{
	DisableColors:    true,
	DisableTimestamp: true,
})

log := NewLogrus(logrusLog)

log.Trace("some Trace log")
log.Debug("some Debug log")
log.Info("some Info log")
log.Warn("some Warn log")
log.Error("some Error log")

log.WithField("RequestID", 1234).Trace("some Trace log")

log.WithFields(map[string]interface{}{
	"RequestID": 1234,
	"Name":      "SomeName",
}).Info("some Info log")

someError := errors.New("an error occurred")
log.WithError(someError).Error("some Error log")
Output:

level=info msg="some Info log"
level=warning msg="some Warn log"
level=error msg="some Error log"
level=info msg="some Info log" Name=SomeName RequestID=1234
level=error msg="some Error log" error="an error occurred"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GormLogger

type GormLogger struct {
	SlowThreshold         time.Duration
	SourceField           string
	SkipErrRecordNotFound bool
	Silent                bool
	// contains filtered or unexported fields
}

GormLogger .

func NewGormLogger

func NewGormLogger(logger Logger) *GormLogger

NewGormLogger .

func (*GormLogger) Error

func (l *GormLogger) Error(ctx context.Context, s string, args ...interface{})

Error .

func (*GormLogger) Info

func (l *GormLogger) Info(ctx context.Context, s string, args ...interface{})

Info .

func (*GormLogger) LogMode

LogMode .

func (*GormLogger) Trace

func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace .

func (*GormLogger) Warn

func (l *GormLogger) Warn(ctx context.Context, s string, args ...interface{})

Warn .

type Logger

type Logger interface {
	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Tracef(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
	WithError(err error) Logger
}

Logger is a basic logging interface

func NewLogrus

func NewLogrus(logger *logrus.Logger) Logger

NewLogrus compatible logger

func NewLogrusEntry

func NewLogrusEntry(entry *logrus.Entry) Logger

NewLogrusEntry compatible logger

type Logrus

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

Logrus compatible wrapper for the logger interface

func (*Logrus) Debug

func (l *Logrus) Debug(args ...interface{})

Debug .

func (*Logrus) Debugf

func (l *Logrus) Debugf(format string, args ...interface{})

Debugf .

func (*Logrus) Error

func (l *Logrus) Error(args ...interface{})

Error .

func (*Logrus) Errorf

func (l *Logrus) Errorf(format string, args ...interface{})

Errorf .

func (*Logrus) Info

func (l *Logrus) Info(args ...interface{})

Info .

func (*Logrus) Infof

func (l *Logrus) Infof(format string, args ...interface{})

Infof .

func (*Logrus) Trace

func (l *Logrus) Trace(args ...interface{})

Trace .

func (*Logrus) Tracef

func (l *Logrus) Tracef(format string, args ...interface{})

Tracef .

func (*Logrus) Warn

func (l *Logrus) Warn(args ...interface{})

Warn .

func (*Logrus) Warnf

func (l *Logrus) Warnf(format string, args ...interface{})

Warnf .

func (*Logrus) WithError

func (l *Logrus) WithError(err error) Logger

WithError .

func (*Logrus) WithField

func (l *Logrus) WithField(key string, value interface{}) Logger

WithField .

func (*Logrus) WithFields

func (l *Logrus) WithFields(fields map[string]interface{}) Logger

WithFields .