log

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package log provides logging mechanism including replaceable Logger interface and its default implementation. Developers may replace default implementation and output level with her desired Logger implementation in a thread-safe manner as below:

type MyLogger struct {}

var _ Logger = (*MyLogger)(nil)

func (*MyLogger) Debug(args ...interface{}) {}

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

func (*MyLogger) Info(args ...interface{}) {}

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

func (*MyLogger) Warn(args ...interface{}) {}

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

func (*MyLogger) Error(args ...interface{}) {}

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

l := &MyLogger{}

// These methods are thread-safe
log.SetLogger(l)
log.SetOutputLevel(log.InfoLevel)

log.Info("Output via new Logger impl.")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug outputs given arguments via pre-set Logger implementation. Logging level must be left with the default setting or be set to DebugLevel via SetOutputLevel().

func Debugf

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

Debugf outputs given arguments with format via pre-set Logger implementation. Logging level must be left with the default setting or be set to DebugLevel via SetOutputLevel().

func Error

func Error(args ...interface{})

Error outputs given arguments via pre-set Logger implementation.

func Errorf

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

Errorf outputs given arguments with format via pre-set Logger implementation.

func Info

func Info(args ...interface{})

Info outputs given arguments via pre-set Logger implementation. Logging level must be set to DebugLevel or InfoLevel via SetOutputLevel().

func Infof

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

Infof outputs given arguments with format via pre-set Logger implementation. Logging level must be set to DebugLevel or InfoLevel via SetOutputLevel().

func SetLogger

func SetLogger(l Logger)

SetLogger receives struct that satisfies Logger interface, and sets this as logger. From this call forward, any call to logging method proxies arguments to corresponding logging method of given Logger. e.g. call to log.Info points to Logger.Info.

This method is "thread-safe."

func SetOutputLevel

func SetOutputLevel(level Level)

SetOutputLevel sets what logging level to output. Application may call logging method any time, but Logger only outputs if the corresponding log level is equal to or higher than the level set here. e.g. When InfoLevel is set, output with Debug() and Debugf() are ignored.

This method is "thread-safe."

DebugLevel is set by default, so this should be explicitly overridden with higher logging level on production environment to avoid printing undesired sensitive data.

func Warn

func Warn(args ...interface{})

Warn outputs given arguments via pre-set Logger implementation. Logging level must be set to DebugLevel, InfoLevel or WarnLevel via SetOutputLevel().

func Warnf

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

Warnf outputs given arguments with format via pre-set Logger implementation. Logging level must be set to DebugLevel, InfoLevel or WarnLevel via SetOutputLevel().

Types

type Level

type Level uint

Level indicates what logging level the output is representing. This typically indicates the severity of a particular logging event.

const (
	// ErrorLevel indicates the error state of events. This must be noted and be fixed.
	// In practical situation, fix may include lowering the corresponding event's log level.
	ErrorLevel Level = iota

	// WarnLevel represents those events that are not critical, but deserves to note.
	// Event with this level may not necessarily be considered as an error;
	// however frequent occurrence deserves developer's attention and may be subject to bug-fix.
	WarnLevel

	// InfoLevel is used to inform what is happening inside the application.
	InfoLevel

	// DebugLevel indicates the output is logged for debugging purpose.
	// This level is not suitable for production usage.
	DebugLevel
)

func (Level) String

func (level Level) String() string

String returns the stringified representation of log level.

type Logger

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{})
}

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.

func GetLogger added in v1.5.0

func GetLogger() Logger

GetLogger returns currently set Logger. Once a preferred Logger implementation is set via log.SetLogger, developer may use its method by calling this package's function: log.Debug, log.Debugf and others.

However when developer wishes to retrieve the Logger instance, this function helps. This is particularly useful in such situation where Logger implementation must be temporarily switched but must be switched back when a task is done. Example follows.

import (
	"github.com/oklahomer/go-sarah/log"
	"io/ioutil"
	stdLogger "log"
	"os"
	"testing"
)

func TestMain(m *testing.M) {
	oldLogger := log.GetLogger()
	defer log.SetLogger(oldLogger)

	l := stdLogger.New(ioutil.Discard, "dummyLog", 0)
	logger := log.NewWithStandardLogger(l)
	log.SetLogger(logger)

	code := m.Run()

	os.Exit(code)
}

func NewWithStandardLogger

func NewWithStandardLogger(l *log.Logger) Logger

NewWithStandardLogger creates an instance of defaultLogger with Go's standard log.Logger. This can be used when implementing Logger interface is too much of a task, but still a bit of modification to defaultLogger is required.

Returning Logger can be fed to SetLogger() to replace old defaultLogger.

Jump to

Keyboard shortcuts

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