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
- func Configure(adapter ILogAdapter)
- func Debug(msg string, args ...Arg)
- func Error(msg string, args ...Arg)
- func Fatal(msg string, args ...Arg)
- func Info(msg string, args ...Arg)
- func Log(level Level, msg string, args ...Arg)
- func Panic(msg string, args ...Arg)
- func Trace(msg string, args ...Arg)
- func Warn(msg string, args ...Arg)
- type AdapterLog
- type AdapterMock
- type AdapterMockRecorder
- type AdapterTest
- type Arg
- type Fields
- type ILogAdapter
- type Level
- type Logger
- func (t *Logger) Debug(msg string, args ...Arg)
- func (t *Logger) Error(msg string, args ...Arg)
- func (t *Logger) Fatal(msg string, args ...Arg)
- func (t *Logger) Info(msg string, args ...Arg)
- func (t *Logger) Log(level Level, msg string, args ...Arg)
- func (t *Logger) Panic(msg string, args ...Arg)
- func (t *Logger) Trace(msg string, args ...Arg)
- func (t *Logger) Warn(msg string, args ...Arg)
Constants ¶
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 Fatal ¶
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 Log ¶
Log log message with level and args There are other shorter syntax for specific level like: Trace, Debug, Info, Warn, Error, Panic, Fatal
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
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 WithFields ¶
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) IsKnowFieldKey ¶
IsKnowFieldKey check if field key is know fields Know keys are: - FieldKeyFormatArgs - FieldKeyMessage - FieldKeyError - FieldKeyLevel
func (Fields) Level ¶
Level extract level in fields return `LevelNone` if there are no level was set or level type
func (Fields) Message ¶
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
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 )
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 (*Logger) Fatal ¶
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) Log ¶
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 ¶
Panic log msg with "panic" level use WithError to attach the error some log engine will call `panic` when logging this level