summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/logr
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mattermost/logr')
-rw-r--r--vendor/github.com/mattermost/logr/v2/buffer.go28
-rw-r--r--vendor/github.com/mattermost/logr/v2/config/config.go8
-rw-r--r--vendor/github.com/mattermost/logr/v2/field.go2
-rw-r--r--vendor/github.com/mattermost/logr/v2/filterstd.go6
-rw-r--r--vendor/github.com/mattermost/logr/v2/sugar.go78
-rw-r--r--vendor/github.com/mattermost/logr/v2/targets/testing.go72
6 files changed, 189 insertions, 5 deletions
diff --git a/vendor/github.com/mattermost/logr/v2/buffer.go b/vendor/github.com/mattermost/logr/v2/buffer.go
new file mode 100644
index 00000000..42bf5255
--- /dev/null
+++ b/vendor/github.com/mattermost/logr/v2/buffer.go
@@ -0,0 +1,28 @@
+package logr
+
+import (
+ "bytes"
+ "sync"
+)
+
+// Buffer provides a thread-safe buffer useful for logging to memory in unit tests.
+type Buffer struct {
+ buf bytes.Buffer
+ mux sync.Mutex
+}
+
+func (b *Buffer) Read(p []byte) (n int, err error) {
+ b.mux.Lock()
+ defer b.mux.Unlock()
+ return b.buf.Read(p)
+}
+func (b *Buffer) Write(p []byte) (n int, err error) {
+ b.mux.Lock()
+ defer b.mux.Unlock()
+ return b.buf.Write(p)
+}
+func (b *Buffer) String() string {
+ b.mux.Lock()
+ defer b.mux.Unlock()
+ return b.buf.String()
+}
diff --git a/vendor/github.com/mattermost/logr/v2/config/config.go b/vendor/github.com/mattermost/logr/v2/config/config.go
index a93b7a25..e01a5514 100644
--- a/vendor/github.com/mattermost/logr/v2/config/config.go
+++ b/vendor/github.com/mattermost/logr/v2/config/config.go
@@ -31,8 +31,8 @@ type TargetFactory func(targetType string, options json.RawMessage) (logr.Target
type FormatterFactory func(format string, options json.RawMessage) (logr.Formatter, error)
type Factories struct {
- targetFactory TargetFactory // can be nil
- formatterFactory FormatterFactory // can be nil
+ TargetFactory TargetFactory // can be nil
+ FormatterFactory FormatterFactory // can be nil
}
var removeAll = func(ti logr.TargetInfo) bool { return true }
@@ -56,7 +56,7 @@ func ConfigureTargets(lgr *logr.Logr, config map[string]TargetCfg, factories *Fa
}
for name, tcfg := range config {
- target, err := newTarget(tcfg.Type, tcfg.Options, factories.targetFactory)
+ target, err := newTarget(tcfg.Type, tcfg.Options, factories.TargetFactory)
if err != nil {
return fmt.Errorf("error creating log target %s: %w", name, err)
}
@@ -65,7 +65,7 @@ func ConfigureTargets(lgr *logr.Logr, config map[string]TargetCfg, factories *Fa
continue
}
- formatter, err := newFormatter(tcfg.Format, tcfg.FormatOptions, factories.formatterFactory)
+ formatter, err := newFormatter(tcfg.Format, tcfg.FormatOptions, factories.FormatterFactory)
if err != nil {
return fmt.Errorf("error creating formatter for log target %s: %w", name, err)
}
diff --git a/vendor/github.com/mattermost/logr/v2/field.go b/vendor/github.com/mattermost/logr/v2/field.go
index 5725d0a1..33342870 100644
--- a/vendor/github.com/mattermost/logr/v2/field.go
+++ b/vendor/github.com/mattermost/logr/v2/field.go
@@ -15,7 +15,7 @@ var (
Space = []byte{' '}
Newline = []byte{'\n'}
Quote = []byte{'"'}
- Colon = []byte{'"'}
+ Colon = []byte{':'}
)
// LogCloner is implemented by `Any` types that require a clone to be provided
diff --git a/vendor/github.com/mattermost/logr/v2/filterstd.go b/vendor/github.com/mattermost/logr/v2/filterstd.go
index 7f38a332..fe917fe5 100644
--- a/vendor/github.com/mattermost/logr/v2/filterstd.go
+++ b/vendor/github.com/mattermost/logr/v2/filterstd.go
@@ -11,6 +11,7 @@ type StdFilter struct {
// is enabled for this filter.
func (lt StdFilter) GetEnabledLevel(level Level) (Level, bool) {
enabled := level.ID <= lt.Lvl.ID
+ stackTrace := level.ID <= lt.Stacktrace.ID
var levelEnabled Level
if enabled {
@@ -33,6 +34,11 @@ func (lt StdFilter) GetEnabledLevel(level Level) (Level, bool) {
levelEnabled = level
}
}
+
+ if stackTrace {
+ levelEnabled.Stacktrace = true
+ }
+
return levelEnabled, enabled
}
diff --git a/vendor/github.com/mattermost/logr/v2/sugar.go b/vendor/github.com/mattermost/logr/v2/sugar.go
index f4f300ee..882f0fd5 100644
--- a/vendor/github.com/mattermost/logr/v2/sugar.go
+++ b/vendor/github.com/mattermost/logr/v2/sugar.go
@@ -117,3 +117,81 @@ func (s Sugar) Fatalf(format string, args ...interface{}) {
func (s Sugar) Panicf(format string, args ...interface{}) {
s.Logf(Panic, format, args...)
}
+
+//
+// K/V style
+//
+
+// With returns a new Sugar logger with the specified key/value pairs added to the
+// fields list.
+func (s Sugar) With(keyValuePairs ...interface{}) Sugar {
+ return s.logger.With(s.argsToFields(keyValuePairs)...).Sugar()
+}
+
+// Tracew outputs at trace level with the specified key/value pairs converted to fields.
+func (s Sugar) Tracew(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Trace, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// Debugw outputs at debug level with the specified key/value pairs converted to fields.
+func (s Sugar) Debugw(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Debug, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// Infow outputs at info level with the specified key/value pairs converted to fields.
+func (s Sugar) Infow(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Info, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// Warnw outputs at warn level with the specified key/value pairs converted to fields.
+func (s Sugar) Warnw(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Warn, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// Errorw outputs at error level with the specified key/value pairs converted to fields.
+func (s Sugar) Errorw(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Error, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// Fatalw outputs at fatal level with the specified key/value pairs converted to fields.
+func (s Sugar) Fatalw(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Fatal, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// Panicw outputs at panic level with the specified key/value pairs converted to fields.
+func (s Sugar) Panicw(msg string, keyValuePairs ...interface{}) {
+ s.logger.Log(Panic, msg, s.argsToFields(keyValuePairs)...)
+}
+
+// argsToFields converts an array of args, possibly containing name/value pairs
+// into a []Field.
+func (s Sugar) argsToFields(keyValuePairs []interface{}) []Field {
+ if len(keyValuePairs) == 0 {
+ return nil
+ }
+
+ fields := make([]Field, 0, len(keyValuePairs))
+ count := len(keyValuePairs)
+
+ for i := 0; i < count; {
+ if fld, ok := keyValuePairs[i].(Field); ok {
+ fields = append(fields, fld)
+ i++
+ continue
+ }
+
+ if i == count-1 {
+ s.logger.Error("invalid key/value pair", Any("arg", keyValuePairs[i]))
+ break
+ }
+
+ // we should have a key/value pair now. The key must be a string.
+ if key, ok := keyValuePairs[i].(string); !ok {
+ s.logger.Error("invalid key for key/value pair", Int("pos", i))
+ } else {
+ fields = append(fields, Any(key, keyValuePairs[i+1]))
+ }
+ i += 2
+ }
+ return fields
+}
diff --git a/vendor/github.com/mattermost/logr/v2/targets/testing.go b/vendor/github.com/mattermost/logr/v2/targets/testing.go
new file mode 100644
index 00000000..ea3df70c
--- /dev/null
+++ b/vendor/github.com/mattermost/logr/v2/targets/testing.go
@@ -0,0 +1,72 @@
+package targets
+
+import (
+ "strings"
+ "sync"
+ "testing"
+
+ "github.com/mattermost/logr/v2"
+ "github.com/mattermost/logr/v2/formatters"
+)
+
+// Testing is a simple log target that writes to a (*testing.T) log.
+type Testing struct {
+ mux sync.Mutex
+ t *testing.T
+}
+
+func NewTestingTarget(t *testing.T) *Testing {
+ return &Testing{
+ t: t,
+ }
+}
+
+// Init is called once to initialize the target.
+func (tt *Testing) Init() error {
+ return nil
+}
+
+// Write outputs bytes to this file target.
+func (tt *Testing) Write(p []byte, rec *logr.LogRec) (int, error) {
+ tt.mux.Lock()
+ defer tt.mux.Unlock()
+
+ if tt.t != nil {
+ s := strings.TrimSpace(string(p))
+ tt.t.Log(s)
+ }
+ return len(p), nil
+}
+
+// Shutdown is called once to free/close any resources.
+// Target queue is already drained when this is called.
+func (tt *Testing) Shutdown() error {
+ tt.mux.Lock()
+ defer tt.mux.Unlock()
+
+ tt.t = nil
+ return nil
+}
+
+// CreateTestLogger creates a logger for unit tests. Log records are output to `(*testing.T).Log`.
+// A new logger is returned along with a method to shutdown the new logger.
+func CreateTestLogger(t *testing.T, levels ...logr.Level) (logger logr.Logger, shutdown func() error) {
+ lgr, _ := logr.New()
+ filter := logr.NewCustomFilter(levels...)
+ formatter := &formatters.Plain{EnableCaller: true}
+ target := NewTestingTarget(t)
+
+ if err := lgr.AddTarget(target, "test", filter, formatter, 1000); err != nil {
+ t.Fail()
+ }
+ shutdown = func() error {
+ err := lgr.Shutdown()
+ if err != nil {
+ target.mux.Lock()
+ target.t.Error("error shutting down test logger", err)
+ target.mux.Unlock()
+ }
+ return err
+ }
+ return lgr.NewLogger(), shutdown
+}