123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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}
- }
|