Files
athens/pkg/log/entry.go
Nicholas Wiersma d932d50232 chore: lint code with golangci-lint (#1828)
* feat: add golangci-lint linting

* chore: fix linter issues

* feat: add linting into the workflow

* docs: update lint docs

* fix: cr suggestions

* fix: remove old formatting and vetting scripts

* fix: add docker make target

* fix: action go caching

* fix: depreciated actions checkout version

* fix: cr suggestion

* fix: cr suggestions

---------

Co-authored-by: Manu Gupta <manugupt1@gmail.com>
2023-02-24 20:39:17 -08:00

67 lines
1.5 KiB
Go

package log
import (
"github.com/gomods/athens/pkg/errors"
"github.com/sirupsen/logrus"
)
// Entry is an abstraction to the
// Logger and the logrus.Entry
// so that *Logger always creates
// an Entry copy which ensures no
// Fields are being overwritten.
type Entry interface {
// Basic Logging Operation
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
// Attach contextual information to the logging entry
WithFields(fields map[string]interface{}) Entry
// SystemErr is a method that disects the error
// and logs the appropriate level and fields for it.
SystemErr(err error)
}
type entry struct {
*logrus.Entry
}
func (e *entry) WithFields(fields map[string]interface{}) Entry {
ent := e.Entry.WithFields(fields)
return &entry{ent}
}
func (e *entry) SystemErr(err error) {
var athensErr errors.Error
if !errors.IsErr(err, athensErr) {
e.Error(err)
return
}
ent := e.WithFields(errFields(athensErr))
switch errors.Severity(err) {
case logrus.WarnLevel:
ent.Warnf("%v", err)
case logrus.InfoLevel:
ent.Infof("%v", err)
case logrus.DebugLevel:
ent.Debugf("%v", err)
default:
ent.Errorf("%v", err)
}
}
func errFields(err errors.Error) logrus.Fields {
f := logrus.Fields{}
f["operation"] = err.Op
f["kind"] = errors.KindText(err)
f["module"] = err.Module
f["version"] = err.Version
f["ops"] = errors.Ops(err)
return f
}