Files
releases/logging/test/test.go
T
Vlad Iovanov c0a692d1ee logging: Remove logger GetFields function (#4116)
This removes the GetFields function from the logger interface, as mentioned in #4114.

GetFields used to be called in one place, creating a new logger using fields from an
http client afaict. I am not sure if my changes have the desired effect in that case,
or how this was desired to work - since the fields of the client are always changing
when making requests.

Fixes #4114.

Signed-off-by: viovanov <vlad@aserto.com>
2022-01-06 07:42:22 +01:00

100 lines
2.0 KiB
Go

package test
import (
"fmt"
"sync"
"github.com/open-policy-agent/opa/logging"
)
// LogEntry represents a log message.
type LogEntry struct {
Level logging.Level
Fields map[string]interface{}
Message string
}
// Logger implementation that buffers messages for test purposes.
type Logger struct {
level logging.Level
fields map[string]interface{}
entries *[]LogEntry
mtx sync.Mutex
}
// New instantiates new Logger.
func New() *Logger {
return &Logger{
level: logging.Info,
entries: &[]LogEntry{},
}
}
// WithFields provides additional fields to include in log output.
// Implemented here primarily to be able to switch between implementations without loss of data.
func (l *Logger) WithFields(fields map[string]interface{}) logging.Logger {
l.mtx.Lock()
defer l.mtx.Unlock()
cp := Logger{
level: l.level,
entries: l.entries,
fields: l.fields,
}
flds := make(map[string]interface{})
for k, v := range cp.fields {
flds[k] = v
}
for k, v := range fields {
flds[k] = v
}
cp.fields = flds
return &cp
}
// Debug buffers a log message.
func (l *Logger) Debug(f string, a ...interface{}) {
l.append(logging.Debug, f, a...)
}
// Info buffers a log message.
func (l *Logger) Info(f string, a ...interface{}) {
l.append(logging.Info, f, a...)
}
// Error buffers a log message.
func (l *Logger) Error(f string, a ...interface{}) {
l.append(logging.Error, f, a...)
}
// Warn buffers a log message.
func (l *Logger) Warn(f string, a ...interface{}) {
l.append(logging.Warn, f, a...)
}
// SetLevel set log level.
func (l *Logger) SetLevel(level logging.Level) {
l.level = level
}
// GetLevel get log level.
func (l *Logger) GetLevel() logging.Level {
return l.level
}
// Entries returns buffered log entries.
func (l *Logger) Entries() []LogEntry {
l.mtx.Lock()
defer l.mtx.Unlock()
return *l.entries
}
func (l *Logger) append(lvl logging.Level, f string, a ...interface{}) {
l.mtx.Lock()
defer l.mtx.Unlock()
*l.entries = append(*l.entries, LogEntry{
Level: lvl,
Fields: l.fields,
Message: fmt.Sprintf(f, a...),
})
}