Skip to content

Commit

Permalink
comments for pub functions
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <[email protected]>
  • Loading branch information
ekexium committed Jan 17, 2023
1 parent 32be4f5 commit 5638294
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions testkit/testutil/loghook.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,21 @@ func (l *logEntry) CheckFieldNotEmpty(t *testing.T, fieldName string) {
require.NotEmpty(t, f.String)
}

// LogHook captures logs, mainly for testing
type LogHook struct {
zapcore.Core
Logs []logEntry
enc zapcore.Encoder
messageFilter string
}

// Write captures the log and save it
func (h *LogHook) Write(entry zapcore.Entry, fields []zapcore.Field) error {
h.Logs = append(h.Logs, logEntry{entry: entry, fields: fields})
return nil
}

// Check implements the string filter
func (h *LogHook) Check(entry zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if len(h.messageFilter) > 0 && !strings.Contains(entry.Message, h.messageFilter) {
return nil
Expand All @@ -88,6 +91,7 @@ func (h *LogHook) encode(entry *logEntry) (string, error) {
return buffer.String(), nil
}

// CheckLogCount is a helper function to assert the number of logs captured
func (h *LogHook) CheckLogCount(t *testing.T, expected int) {
logsStr := make([]string, len(h.Logs))
for i, item := range h.Logs {
Expand All @@ -99,6 +103,7 @@ func (h *LogHook) CheckLogCount(t *testing.T, expected int) {
require.Len(t, logsStr, expected)
}

// WithLogHook is a helper function to use with LogHook. It returns a context whose logger is replaced with the hook.
func WithLogHook(ctx context.Context, t *testing.T, msgFilter string) (newCtx context.Context, hook *LogHook) {
conf := &log.Config{Level: os.Getenv("log_level"), File: log.FileLogConfig{}}
_, r, _ := log.InitLogger(conf)
Expand Down

0 comments on commit 5638294

Please sign in to comment.