micro/logger/helper.go
Vasiliy Tolstov 3543b275e0
fix log level helper (#1342)
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-03-13 17:36:42 +03:00

115 lines
2.6 KiB
Go

package logger
import (
"os"
)
type Helper struct {
Logger
fields map[string]interface{}
}
func NewHelper(log Logger) *Helper {
return &Helper{Logger: log}
}
func (h *Helper) Info(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(InfoLevel) {
return
}
h.Logger.Fields(h.fields).Log(InfoLevel, args...)
}
func (h *Helper) Infof(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(InfoLevel) {
return
}
h.Logger.Fields(h.fields).Logf(InfoLevel, template, args...)
}
func (h *Helper) Trace(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(TraceLevel) {
return
}
h.Logger.Fields(h.fields).Log(TraceLevel, args...)
}
func (h *Helper) Tracef(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(TraceLevel) {
return
}
h.Logger.Fields(h.fields).Logf(TraceLevel, template, args...)
}
func (h *Helper) Debug(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(DebugLevel) {
return
}
h.Logger.Fields(h.fields).Log(DebugLevel, args...)
}
func (h *Helper) Debugf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(DebugLevel) {
return
}
h.Logger.Fields(h.fields).Logf(DebugLevel, template, args...)
}
func (h *Helper) Warn(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(WarnLevel) {
return
}
h.Logger.Fields(h.fields).Log(WarnLevel, args...)
}
func (h *Helper) Warnf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(WarnLevel) {
return
}
h.Logger.Fields(h.fields).Logf(WarnLevel, template, args...)
}
func (h *Helper) Error(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(ErrorLevel) {
return
}
h.Logger.Fields(h.fields).Log(ErrorLevel, args...)
}
func (h *Helper) Errorf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(ErrorLevel) {
return
}
h.Logger.Fields(h.fields).Logf(ErrorLevel, template, args...)
}
func (h *Helper) Fatal(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(FatalLevel) {
return
}
h.Logger.Fields(h.fields).Log(FatalLevel, args...)
os.Exit(1)
}
func (h *Helper) Fatalf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(FatalLevel) {
return
}
h.Logger.Fields(h.fields).Logf(FatalLevel, template, args...)
os.Exit(1)
}
func (h *Helper) WithError(err error) *Helper {
fields := copyFields(h.fields)
fields["error"] = err
return &Helper{Logger: h.Logger, fields: fields}
}
func (h *Helper) WithFields(fields map[string]interface{}) *Helper {
nfields := copyFields(fields)
for k, v := range h.fields {
nfields[k] = v
}
return &Helper{Logger: h.Logger, fields: nfields}
}