2020-02-29 03:31:59 +03:00
|
|
|
package logger
|
|
|
|
|
2020-03-13 17:36:42 +03:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
2020-03-04 13:46:01 +00:00
|
|
|
|
2020-02-29 03:31:59 +03:00
|
|
|
type Helper struct {
|
|
|
|
Logger
|
2020-03-03 11:07:38 +03:00
|
|
|
fields map[string]interface{}
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHelper(log Logger) *Helper {
|
2020-03-03 11:07:38 +03:00
|
|
|
return &Helper{Logger: log}
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Info(args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Log(InfoLevel, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Infof(template string, args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(InfoLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Logf(InfoLevel, template, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Trace(args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(TraceLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Log(TraceLevel, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Tracef(template string, args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(TraceLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Logf(TraceLevel, template, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Debug(args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(DebugLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Log(DebugLevel, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Debugf(template string, args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(DebugLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Logf(DebugLevel, template, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Warn(args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(WarnLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Log(WarnLevel, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Warnf(template string, args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(WarnLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Logf(WarnLevel, template, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Error(args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(ErrorLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Log(ErrorLevel, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Errorf(template string, args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(ErrorLevel) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
h.Logger.Fields(h.fields).Logf(ErrorLevel, template, args...)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Fatal(args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(FatalLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Logger.Fields(h.fields).Log(FatalLevel, args...)
|
2020-03-04 13:46:01 +00:00
|
|
|
os.Exit(1)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Fatalf(template string, args ...interface{}) {
|
2020-03-11 20:55:39 +03:00
|
|
|
if !h.Logger.Options().Level.Enabled(FatalLevel) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Logger.Fields(h.fields).Logf(FatalLevel, template, args...)
|
2020-03-04 13:46:01 +00:00
|
|
|
os.Exit(1)
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) WithError(err error) *Helper {
|
2020-03-03 11:07:38 +03:00
|
|
|
fields := copyFields(h.fields)
|
|
|
|
fields["error"] = err
|
|
|
|
return &Helper{Logger: h.Logger, fields: fields}
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|
2020-02-29 03:39:41 +03:00
|
|
|
|
|
|
|
func (h *Helper) WithFields(fields map[string]interface{}) *Helper {
|
2020-03-03 11:07:38 +03:00
|
|
|
nfields := copyFields(fields)
|
|
|
|
for k, v := range h.fields {
|
|
|
|
nfields[k] = v
|
|
|
|
}
|
|
|
|
return &Helper{Logger: h.Logger, fields: nfields}
|
2020-02-29 03:39:41 +03:00
|
|
|
}
|