# 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
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
# Functions
Configure configure default log for log package.
Debug log message with debug level and arguments.
Error log message with "error" level use WithError to attach the error.
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.
Info log message with Info level and arguments.
Log log message with level and args There are other shorter syntax for specific level like: Trace, Debug, Info, Warn, Error, Panic, Fatal.
NewAdapterLog create `AdapterLog``.
NewAdapterMock creates a new mock instance.
NewAdapterTest create `AdapterTest` by provide a function that help inspect the `fields` which were passed by `Log` method.
NewLogger create logger instance.
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.
Trace log message with "trace" level and arguments.
Warn log message with "warn" level and arguments.
WithError will attach the error into the log item fields.
WithField attach field into a log item.
WithFields attach multiple fields into a log item.
WithFormatArg if the message is a format string, use this method to pass the format arguments.
# Constants
FieldKeyError key of log error field.
FieldKeyFormatArgs key of format arguments field format arguments is a list of arguments that can be passed into the `fmt.Sprintf`.
FieldKeyLevel key of log level field.
FieldKeyMessage key of log message field.
LevelDebug log level debug.
LevelError log level error.
LevelFatal log level fatal.
LevelInfo log level info.
LevelNone log level that represent as not set level.
LevelPanic log level panic.
LevelTrace log level trace.
LevelWarn log level warn.
# Structs
AdapterLog will be used as default log adapter.
AdapterMock is a mock of ILogAdapter interface.
AdapterMockRecorder is the mock recorder for LogAdapterMock.
AdapterTest is a stub help on testing.
Logger provide common logging methods the logger will call `ILogAdapter.Log` to write actual log item.
# Interfaces
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`.