Skip to content
forked from nitishm/logger

A wrapper around https://github.com/sirupsen/logrus that allows setting default fields, and supports adding and removing configurable fields

License

Notifications You must be signed in to change notification settings

sanamsarath/logger

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logger

A wrapper around https://github.com/sirupsen/logrus that allows setting default fields, and supports adding and removing configurable fields.

GoDoc

Usage

import (
	"github.com/nitishm/logger"
	log "github.com/sirupsen/logrus"
)

func main() {
  level := "info"
	l := logger.New()
	l.Logger.SetLevel(level)
	l.Logger.SetFormatter(&log.JSONFormatter{})
	l.Logger.SetReportCaller(true)
  ...
}

func() {
  ...
  defer l.RemoveFieldsByKey("label_selector")
  l.AddField("label_selectors", label.String())
  ...
  f := log.Fields{
      "num_bytes":   summary.NumBytes,
      "duration":    summary.Duration,
  }
  defer l.RemoveFields(f)
  l.AddFields(f)
  ...
  err := doSomething(context.TODO(), input)
  if err != nil {
    err = c.l.WrapAndPrintWithError(err, fmt.Sprintf("Failed to CreateOrUpdate Capture instance for ID %s", c.id))
    return err
  }
  ...
}

Supported Methods

// Encapsulates all supported logrus methods
log.Ext1FieldLogger
// Fields returns the set of configured non-default fields
Fields() log.Fields
// Defaults returns the set of default fields 
Defaults() log.Fields
// SetDefaults allows the user to set a default set of fields
// that are included in every log message
SetDefaults(defaults log.Fields)
// AddField allows the user to add a single key/value to the 
// non-default set of fields
AddField(key string, value interface{})
// AddFields allows the user to add multipme key/values to the 
// non-default set of fields
AddFields(fields log.Fields)
// RemoveFieldsByKey allows the user to remove multiple non-default
// fields using one or more key values
RemoveFieldsByKey(key ...string)
// RemoveFields allows the user to remove a set of fields
// This is useful when the user wishes to add a set of fields
// and defer their deletion subsequently after the function returns.
RemoveFields(fields log.Fields)
// ResetFields resets all the fields including the default and non-default
// fields.
ResetFields()
// WrapAndPrintWithError is a helper that allows the user to wrap an error
// using the errors package and log that to the output 
WrapAndPrintWithError(err error, format string, args ...interface{}) error
// PrintWithError allows a user to log the error output along with the 
// error value to the output
PrintWithError(err error, format string, args ...interface{})

About

A wrapper around https://github.com/sirupsen/logrus that allows setting default fields, and supports adding and removing configurable fields

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Go 100.0%