Skip to content

Commit

Permalink
Improve docs, add ExecutionIDFrom(ctx) method to Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Linford committed Jun 23, 2021
1 parent 54fca0e commit 3673c74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 14 additions & 4 deletions yall.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ const (
MissingExecutionID = "missing_execution_id"
)

// Logger is the interface of a logger, that has methods to log with a context or without.
// With is used to decorate the logger with key-value pairs.
// ExecutionIDFrom allows to extract the execution id from the context, if present, and depends on the concrete logger
// implementation.
type Logger interface {
contextLogger
noContextLogger
ContextLogger
NoContextLogger
With(args ...interface{}) Logger
ExecutionIDFrom(ctx context.Context) string
}

type contextLogger interface {
// ContextLogger is the interface of a logger, that has methods to log with a context.
// keysAndValues has to be formed by pairs of key and value, or single Fields.
type ContextLogger interface {
Fatal(ctx context.Context, msg string, keysAndValues ...interface{})
Panic(ctx context.Context, msg string, keysAndValues ...interface{})
Error(ctx context.Context, msg string, keysAndValues ...interface{})
Expand All @@ -28,7 +35,9 @@ type contextLogger interface {
Debug(ctx context.Context, msg string, keysAndValues ...interface{})
}

type noContextLogger interface {
// NoContextLogger is the interface of a logger, that has methods to log without a context.
// keysAndValues has to be formed by pairs of key and value, or single Fields.
type NoContextLogger interface {
Fatalnc(msg string, keysAndValues ...interface{})
Panicnc(msg string, keysAndValues ...interface{})
Errornc(msg string, keysAndValues ...interface{})
Expand All @@ -37,6 +46,7 @@ type noContextLogger interface {
Debugnc(msg string, keysAndValues ...interface{})
}

// Field is a loggable object, that can be used instead of key value pairs
type Field struct {
Name string
Value interface{}
Expand Down
9 changes: 9 additions & 0 deletions zaplogger/zap_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ func TestZapLoggerCreate(t *testing.T) {
logger.Info(nil, "test")
}
}

func TestExecutionIDFrom(t *testing.T) {
ctx := context.WithValue(context.Background(), yall.ExecutionIDKey, "request_id_test123")
logger, _ := zaplogger.NewLogger("test")

executionID := logger.ExecutionIDFrom(ctx)
assert.NotEmpty(t, executionID)
assert.EqualValues(t, "request_id_test123", executionID)
}

0 comments on commit 3673c74

Please sign in to comment.