Skip to content
Oklahomer edited this page Dec 26, 2017 · 5 revisions

Overall

Golnag's standard logging library, log package, does not provide Logger interface. The absence of common interface and its minimal implementation lead some developers to a search for alternate logging library or for a better logging architecture. Many logging libraries are introduced, but none is yet considered as De-facto.

This project, instead of choosing one such logging library, has a log.Logger interface and its default implementation that simply wraps standard log library. A developer may use preferred logging library by wrapping one to satisfy log.Logger interface.

Default Logger

By default log.Logger's default implementation, log.defaultLogger, is set. This instance is pre-declared on this package space, so developers do not need any additional effort to use this default implementation; direct call to this package's functions ranging from log.Debug to log.Errorf works just fine.

import (
	"github.com/oklahomer/go-sarah/log"
)

log.Info("This is log.Info's output.")
log.Infof("This is log.Infof's output. This is %s, now.", time.Now().String())
// Outputs are as below:
// sarah 2017/12/22 10:27:59 /usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:746: [INFO] This is log.Info's output.
// sarah 2017/12/22 10:27:59 /usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:746: [INFO] This is log.Infof's output. This is 2017-12-22 10:27:59.546768185 +0900 JST m=+0.000695168, now.

Its underlying logger is golang's standard logger, and is initialized as below:

log.New(os.Stdout, "sarah ", log.LstdFlags|log.Llongfile))

To customize the standard logger's behavior, developer may simply assign one via log.NewWithStandardLogger:

import (
        stdLog "log"
        sarahLogger "github.com/oklahomer/go-sarah/log"
)

// Create standard logger's instance with preferred settings.
stdLogger := stdLog.New(os.Stdout, "myApp ", log.Lshortfile)

// Return log.defaultLogger instance as log.Logger interface.
myLogger := sarahLogger.NewWithStandardLogger(stdLogger)

// From this point, standard logger with preferred setting is used.
// This uses mutex to switch standard logger in a thread-safe manner.
sarahLogger.SetLogger(myLogger)

sarahLogger.Info("Underlying logger is switched!!")

Customizable Interface

The interface is as below:

// Logger defines the interface that can be used as logging tool in this application.
// Developer may provide a customized logger via SetLogger to modify behavior.
// By default, instance of defaultLogger is set as default Logger just like http's DefaultClient.
type Logger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})

	Info(args ...interface{})
	Infof(format string, args ...interface{})

	Warn(args ...interface{})
	Warnf(format string, args ...interface{})

	Error(args ...interface{})
	Errorf(format string, args ...interface{})
}

Developers are free to replace default logger with any struct that satisfies this Logger interface. To use third party logging library with different interface, simply create an adapter struct that wraps preferred logger.

Clone this wiki locally