Skip to content

Commit

Permalink
feat: add Group method to simplify group logger usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Feb 8, 2023
1 parent 903f5ec commit 032649f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cilog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ func Can(l Logger, capability Capability) bool {
return exists
}

// Group starts a group logger.
// If the given logger doesn't support group logging,
// the original logger will be returned and a noop finish function will be returned.
func Group(l Logger, params GroupLogParams) (Logger, func()) {
if !Can(l, CapabilityGroupLog) {
return l, func() {}
}

return l.GroupLog(params)
}

// GroupLogParams specifies the parameter for GroupLog.
type GroupLogParams struct {
// Name - name of the group
Expand Down
7 changes: 7 additions & 0 deletions cilog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ func TestGet(t *testing.T) {
assert.IsType(t, &azurePipelineT{}, Get(ci.AzurePipelines))
assert.IsType(t, &genericT{}, Get(ci.Custom))
}

func TestGroup_GroupUnsupported(t *testing.T) {
l := &Mute{}
subLogger, finish := Group(l, GroupLogParams{Name: "group"})
assert.NotNil(t, finish)
assert.Equal(t, l, subLogger)
}

0 comments on commit 032649f

Please sign in to comment.