log

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2019 License: MIT Imports: 7 Imported by: 1

README

Logging

This package provide methods that help in logging you can change the log engine by changing the log adapter which implements the ILogAdapter interface

Design

log package design

Usage

Install
go get github.com/jkaveri/goabs/log
# install adapter
# for example, if you use logrus
go get github.com/jkaveri/goabs/log/adapter-logrus
Default

You can start using log package with default configuration which will use AdapterLog as an adapter which uses "log" package as log engine

func man() {
	// simple log with level "info"
	log.Info("this is an log message")
	// log with error
	log.Error(
		"this is an error log message", 
		log.WithError(errors.New("simple eror")),
	)
	// log with custom field
	log.Trace(
		"this is an trace message with username",
		log.WithField("username", "jkaveri"),
		log.WithField("repo", "github.com/jkaveri/goabs"),
	)
}
Use with logrus
    logger := logrus.New()
    logger.SetLevel(logrus.InfoLevel)
    log.Configure(
		    logadapter.NewAdapterLogrus(logger),
	  )
	  
    log.Info("this is log message")
    
    log.Info(
        "this is formatted message: %d",
        log.WithFormatArg(10),
    )
    
    log.Info(
        "this is full feature of %s",
        log.WithFormatArg("logging"),
        log.WithFields(log.Fields{
          "username": "some username",
          "score":    15.2,
        }),
        log.WithField("age", 10),
        log.WithError(errors.New("test error")),
    )
Use another log engine

You can use any log engine which you like and familiar with. Just implement the ILogAdapter

type ILogAdapter interface {
	Log(fields Fields)
}

When you implemented the adapter you can configure log package to use your adapter:

log.Configure(mylogadapter.NewAdapter())
Examples

Documentation

Overview

Package log provide common logging methods and `ILogAdapter` interface that help we wrap another log engine package such as `logrus`, `zap` or `zerolog` we can archive the flexibility by wrapping the log engine package. In another word our application can be easy to switch to another log engine when we want and it should not have any impact

Index

Constants

View Source
const (
	// FieldKeyLevel key of log level field
	FieldKeyLevel = "level"
	// FieldKeyMessage key of log message field
	FieldKeyMessage = "message"
	// FieldKeyError key of log error field
	FieldKeyError = "error"
	// FieldKeyFormatArgs key of format arguments field
	// format arguments is a list of arguments that can
	// be passed into the `fmt.Sprintf`
	FieldKeyFormatArgs = "@@fargs"
)

Variables

This section is empty.

Functions

func Configure

func Configure(adapter ILogAdapter)

Configure configure default log for log package

func Debug

func Debug(msg string, args ...Arg)

Debug log message with debug level and arguments

func Error

func Error(msg string, args ...Arg)

Error log message with "error" level use WithError to attach the error

func Fatal

func Fatal(msg string, args ...Arg)

Fatal log message with "fatal" level and arguments use WithError to attach the error some log engine may call os.Exit function after log the message

func Info

func Info(msg string, args ...Arg)

Info log message with Info level and arguments

func Log

func Log(level Level, msg string, args ...Arg)

Log log message with level and args There are other shorter syntax for specific level like: Trace, Debug, Info, Warn, Error, Panic, Fatal

func Panic

func Panic(msg string, args ...Arg)

Panic log message with "panic" level and arguments use WithError to attach the error some log engine may call panic function after log the message

func Trace

func Trace(msg string, args ...Arg)

Trace log message with "trace" level and arguments

func Warn

func Warn(msg string, args ...Arg)

Warn log message with "warn" level and arguments

Types

type AdapterLog

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

AdapterLog will be used as default log adapter

func NewAdapterLog

func NewAdapterLog(out io.Writer, prefix string, flag int) *AdapterLog

NewAdapterLog create `AdapterLog“. The `out` destination to which log data will be written. default: `os.Stderr` The `prefix` appears at the beginning of each generated log line. The `flag` argument defines the logging properties. refer: https://golang.org/pkg/log/#pkg-constants

func (*AdapterLog) Log

func (t *AdapterLog) Log(fields Fields)

Log will use go native `"log"` package to write log from `fields`

type AdapterMock

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

AdapterMock is a mock of ILogAdapter interface

func NewAdapterMock

func NewAdapterMock(ctrl *gomock.Controller) *AdapterMock

NewAdapterMock creates a new mock instance

func (*AdapterMock) EXPECT

func (m *AdapterMock) EXPECT() *AdapterMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*AdapterMock) Log

func (m *AdapterMock) Log(fields Fields)

Log mocks base method

type AdapterMockRecorder

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

AdapterMockRecorder is the mock recorder for LogAdapterMock

func (*AdapterMockRecorder) Log

func (mr *AdapterMockRecorder) Log(fields interface{}) *gomock.Call

Log indicates an expected call of Log

type AdapterTest

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

AdapterTest is a stub help on testing

func (*AdapterTest) Log

func (t *AdapterTest) Log(fields Fields)

Log implement `ILogAdapter` interface the fields pass-in will be pass to the logFunc which was initiate in the `NewAdapterTest` method

type Arg

type Arg func(fields Fields)

Arg log argument function

func WithError

func WithError(err error) Arg

WithError will attach the error into the log item fields

func WithField

func WithField(key string, val interface{}) Arg

WithField attach field into a log item

func WithFields

func WithFields(fields Fields) Arg

WithFields attach multiple fields into a log item

func WithFormatArg

func WithFormatArg(args ...interface{}) Arg

WithFormatArg if the message is a format string, use this method to pass the format arguments. when serialize the log message this format arguments and the format string will be passed into the `fmt.Sprintf` method

type Fields

type Fields map[string]interface{}

Fields fields contains all logging data

func (Fields) DeleteAllKnowFields

func (d Fields) DeleteAllKnowFields()

DeleteAllKnowFields delete all fields which are knew fields Know keys are: - FieldKeyFormatArgs - FieldKeyMessage - FieldKeyError - FieldKeyLevel

func (Fields) Error

func (d Fields) Error() error

Error extract error from Fields return nil if not exist

func (Fields) IsKnowFieldKey

func (d Fields) IsKnowFieldKey(key string) bool

IsKnowFieldKey check if field key is know fields Know keys are: - FieldKeyFormatArgs - FieldKeyMessage - FieldKeyError - FieldKeyLevel

func (Fields) Level

func (d Fields) Level() Level

Level extract level in fields return `LevelNone` if there are no level was set or level type

func (Fields) Message

func (d Fields) Message() string

Message extract log message in fields return empty string if log message haven't set yet. return formatted string (like `fmt.Sprintf`) if the message is format string and user has attached the format arguments by using `WithFormatArgs` method

func (Fields) Rest

func (d Fields) Rest() Fields

Rest return all log data in Fields except "known-fields" known fields are: Error, Message, Level, FormatArgs

func (Fields) String

func (d Fields) String() string

String serialize the Fields to a string return a string in this format: ```text [level="%s"][ message="%s"][ error="%s"][...[ key="%s"] ``` log message will use the `fields.Message()` method.

type ILogAdapter

type ILogAdapter interface {
	Log(fields Fields)
}

ILogAdapter a log adapter is a connector between `goabs/log` with a log engine for example: If you want logging with `logrus` you can implement `ILogAdapter` that use `logrus` API to write log based on `log.Fields`

func NewAdapterTest

func NewAdapterTest(logFunc func(fields Fields)) ILogAdapter

NewAdapterTest create `AdapterTest` by provide a function that help inspect the `fields` which were passed by `Log` method

type Level

type Level int

Level logging level type

const (
	// LevelNone log level that represent as not set level
	LevelNone Level = iota
	// LevelTrace log level trace
	LevelTrace
	// LevelDebug log level debug
	LevelDebug
	// LevelInfo log level info
	LevelInfo
	// LevelWarn log level warn
	LevelWarn
	// LevelError log level error
	LevelError
	// LevelPanic log level panic
	LevelPanic
	// LevelFatal log level fatal
	LevelFatal
)

func (Level) String

func (t Level) String() string

String return log level name

type Logger

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

Logger provide common logging methods the logger will call `ILogAdapter.Log` to write actual log item

func NewLogger

func NewLogger(adapter ILogAdapter) *Logger

NewLogger create logger instance

func (*Logger) Debug

func (t *Logger) Debug(msg string, args ...Arg)

Debug log msg with "debug" level

func (*Logger) Error

func (t *Logger) Error(msg string, args ...Arg)

Error log msg with "error" level use WithError to attach the error

func (*Logger) Fatal

func (t *Logger) Fatal(msg string, args ...Arg)

Fatal log msg with "fatal" level use WithError to attach the error some log engine will call `os.Exit` when logging this level

func (*Logger) Info

func (t *Logger) Info(msg string, args ...Arg)

Info log msg with "info" level

func (*Logger) Log

func (t *Logger) Log(level Level, msg string, args ...Arg)

Log a generic method that write log with level and msg you can pass-in the list of logging arguments to add more information into the log item Use some sugar method like: WithField, WithError, WithFormatArgs the level and the msg cannot be override by `Arg` function

func (*Logger) Panic

func (t *Logger) Panic(msg string, args ...Arg)

Panic log msg with "panic" level use WithError to attach the error some log engine will call `panic` when logging this level

func (*Logger) Trace

func (t *Logger) Trace(msg string, args ...Arg)

Trace log msg with "trace" level

func (*Logger) Warn

func (t *Logger) Warn(msg string, args ...Arg)

Warn log msg with "warn" level

Jump to

Keyboard shortcuts

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