-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terminal/logflags: Added
SetLoggerFactory(LoggerFactory)
(#3257)
* terminal/logflags: Added `SetLoggerFactory(LoggerFactory)` This change will enable people who want to embed Delve into their applications to adjust the logging better to their needs. * terminal/logflags: Added `SetLoggerFactory(LoggerFactory)` Added changes from code review. * terminal/logflags: Added `SetLoggerFactory(LoggerFactory)` Reworked requested changes. * terminal/logflags: Added `SetLoggerFactory(LoggerFactory)` Reworked requested changes.
- Loading branch information
Showing
8 changed files
with
243 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package logflags | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestMakeLogger_usingLoggerFactory(t *testing.T) { | ||
if loggerFactory != nil { | ||
t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory) | ||
} | ||
defer func() { | ||
loggerFactory = nil | ||
}() | ||
if logOut != nil { | ||
t.Fatalf("expected logOut to be nil; but was <%v>", logOut) | ||
} | ||
logOut = &bufferWriter{} | ||
defer func() { | ||
logOut = nil | ||
}() | ||
|
||
expectedLogger := &logrusLogger{} | ||
SetLoggerFactory(func(flag bool, fields Fields, out io.Writer) Logger { | ||
if flag != true { | ||
t.Fatalf("expected flag to be <%v>; but was <%v>", true, flag) | ||
} | ||
if len(fields) != 1 || fields["foo"] != "bar" { | ||
t.Fatalf("expected fields to be {'foo':'bar'}; but was <%v>", fields) | ||
} | ||
if out != logOut { | ||
t.Fatalf("expected out to be <%v>; but was <%v>", logOut, out) | ||
} | ||
return expectedLogger | ||
}) | ||
|
||
actual := makeLogger(true, Fields{"foo": "bar"}) | ||
if actual != expectedLogger { | ||
t.Fatalf("expected actual to <%v>; but was <%v>", expectedLogger, actual) | ||
} | ||
} | ||
|
||
func TestMakeLogger_usingDefaultBehavior(t *testing.T) { | ||
if loggerFactory != nil { | ||
t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory) | ||
} | ||
if logOut != nil { | ||
t.Fatalf("expected logOut to be nil; but was <%v>", logOut) | ||
} | ||
logOut = &bufferWriter{} | ||
defer func() { | ||
logOut = nil | ||
}() | ||
|
||
actual := makeLogger(false, Fields{"foo": "bar"}) | ||
|
||
actualEntry, expectedType := actual.(*logrusLogger) | ||
if !expectedType { | ||
t.Fatalf("expected actual to be of type <%v>; but was <%v>", reflect.TypeOf((*logrus.Entry)(nil)), reflect.TypeOf(actualEntry)) | ||
} | ||
if actualEntry.Entry.Logger.Level != logrus.ErrorLevel { | ||
t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", logrus.ErrorLevel, actualEntry.Logger.Level) | ||
} | ||
if actualEntry.Entry.Logger.Out != logOut { | ||
t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, actualEntry.Logger.Out) | ||
} | ||
if actualEntry.Entry.Logger.Formatter != textFormatterInstance { | ||
t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be <%v>; but was <%v>", textFormatterInstance, actualEntry.Logger.Formatter) | ||
} | ||
if len(actualEntry.Entry.Data) != 1 || actualEntry.Entry.Data["foo"] != "bar" { | ||
t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", actualEntry.Data) | ||
} | ||
} | ||
|
||
func TestMakeLogger_usingDefaultBehaviorAndFlagged(t *testing.T) { | ||
if loggerFactory != nil { | ||
t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory) | ||
} | ||
if logOut != nil { | ||
t.Fatalf("expected logOut to be nil; but was <%v>", logOut) | ||
} | ||
logOut = &bufferWriter{} | ||
defer func() { | ||
logOut = nil | ||
}() | ||
|
||
actual := makeLogger(true, Fields{"foo": "bar"}) | ||
|
||
actualEntry, expectedType := actual.(*logrusLogger) | ||
if !expectedType { | ||
t.Fatalf("expected actual to be of type <%v>; but was <%v>", reflect.TypeOf((*logrus.Entry)(nil)), reflect.TypeOf(actualEntry)) | ||
} | ||
if actualEntry.Entry.Logger.Level != logrus.DebugLevel { | ||
t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", logrus.DebugLevel, actualEntry.Logger.Level) | ||
} | ||
if actualEntry.Entry.Logger.Out != logOut { | ||
t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, actualEntry.Logger.Out) | ||
} | ||
if actualEntry.Entry.Logger.Formatter != textFormatterInstance { | ||
t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be <%v>; but was <%v>", textFormatterInstance, actualEntry.Logger.Formatter) | ||
} | ||
if len(actualEntry.Entry.Data) != 1 || actualEntry.Entry.Data["foo"] != "bar" { | ||
t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", actualEntry.Data) | ||
} | ||
} | ||
|
||
type bufferWriter struct { | ||
bytes.Buffer | ||
} | ||
|
||
func (bw bufferWriter) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package logflags | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"io" | ||
) | ||
|
||
// Logger represents a generic interface for logging inside of | ||
// Delve codebase. | ||
type Logger interface { | ||
// WithField returns a new Logger enriched with the given field. | ||
WithField(key string, value interface{}) Logger | ||
// WithFields returns a new Logger enriched with the given fields. | ||
WithFields(fields Fields) Logger | ||
// WithError returns a new Logger enriched with the given error. | ||
WithError(err error) Logger | ||
|
||
Debugf(format string, args ...interface{}) | ||
Infof(format string, args ...interface{}) | ||
Printf(format string, args ...interface{}) | ||
Warnf(format string, args ...interface{}) | ||
Warningf(format string, args ...interface{}) | ||
Errorf(format string, args ...interface{}) | ||
Fatalf(format string, args ...interface{}) | ||
Panicf(format string, args ...interface{}) | ||
|
||
Debug(args ...interface{}) | ||
Info(args ...interface{}) | ||
Print(args ...interface{}) | ||
Warn(args ...interface{}) | ||
Warning(args ...interface{}) | ||
Error(args ...interface{}) | ||
Fatal(args ...interface{}) | ||
Panic(args ...interface{}) | ||
|
||
Debugln(args ...interface{}) | ||
Infoln(args ...interface{}) | ||
Println(args ...interface{}) | ||
Warnln(args ...interface{}) | ||
Warningln(args ...interface{}) | ||
Errorln(args ...interface{}) | ||
Fatalln(args ...interface{}) | ||
Panicln(args ...interface{}) | ||
} | ||
|
||
// LoggerFactory is used to create new Logger instances. | ||
// SetLoggerFactory can be used to configure it. | ||
// | ||
// The given parameters fields and out can be both be nil. | ||
type LoggerFactory func(flag bool, fields Fields, out io.Writer) Logger | ||
|
||
var loggerFactory LoggerFactory | ||
|
||
// SetLoggerFactory will ensure that every Logger created by this package, will be now created | ||
// by the given LoggerFactory. Default behavior will be a logrus based Logger instance using DefaultFormatter. | ||
func SetLoggerFactory(lf LoggerFactory) { | ||
loggerFactory = lf | ||
} | ||
|
||
// Fields type wraps many fields for Logger | ||
type Fields map[string]interface{} | ||
|
||
type logrusLogger struct { | ||
*logrus.Entry | ||
} | ||
|
||
func (l *logrusLogger) WithField(key string, value interface{}) Logger { | ||
return &logrusLogger{l.Entry.WithField(key, value)} | ||
} | ||
|
||
func (l *logrusLogger) WithFields(fields Fields) Logger { | ||
return &logrusLogger{l.Entry.WithFields(logrus.Fields(fields))} | ||
} | ||
|
||
func (l *logrusLogger) WithError(err error) Logger { | ||
return &logrusLogger{l.Entry.WithError(err)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.