Skip to content

Commit

Permalink
feat: format log output
Browse files Browse the repository at this point in the history
  • Loading branch information
odsod committed Jan 13, 2022
1 parent 0e09f68 commit 2427a84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .mage/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.einride.tech/mage-tools/targets/mgmarkdownfmt"

// mage:import
"go.einride.tech/mage-tools/targets/mgcocogitto"
"go.einride.tech/mage-tools/targets/mgconvco"

// mage:import
"go.einride.tech/mage-tools/targets/mggo"
Expand All @@ -38,7 +38,7 @@ func init() {

func All() {
mg.Deps(
mgcocogitto.CogCheck,
mg.F(mgconvco.ConvcoCheck, "origin/main..HEAD"),
mggolangcilint.GolangciLint,
mggoreview.Goreview,
mgmarkdownfmt.FormatMarkdown,
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ include .mage/tools.mk
all: $(mage)
@$(mage) all

.PHONY: cog-check
cog-check: $(mage)
@$(mage) cogCheck
.PHONY: convco-check
convco-check: $(mage)
ifndef rev
$(error missing argument rev="...")
endif
@$(mage) convcoCheck $(rev)

.PHONY: format-markdown
format-markdown: $(mage)
Expand Down
28 changes: 16 additions & 12 deletions mglog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import (
"fmt"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
)

// Logger returns a standard logger.
func Logger(name string) logr.Logger {
return logr.New(&sink{}).WithName(name)
return logr.New(&sink{
name: name,
formatter: funcr.NewFormatter(funcr.Options{
RenderBuiltinsHook: func(kvList []interface{}) []interface{} {
// Don't render builtins.
return nil
},
}),
})
}

type sink struct {
name string
formatter funcr.Formatter
name string
}

func (s *sink) Init(info logr.RuntimeInfo) {
Expand All @@ -23,19 +33,13 @@ func (s *sink) Enabled(level int) bool {
}

func (s *sink) Info(level int, msg string, keysAndValues ...interface{}) {
if len(keysAndValues) > 0 {
fmt.Printf("[%s] %s (%v)\n", s.name, msg, keysAndValues)
} else {
fmt.Printf("[%s] %s\n", s.name, msg)
}
_, args := s.formatter.FormatInfo(level, msg, keysAndValues)
fmt.Printf("[%s] %s%s\n", s.name, msg, args)
}

func (s *sink) Error(err error, msg string, keysAndValues ...interface{}) {
if len(keysAndValues) > 0 {
fmt.Printf("[%s - ERROR] %s (%v)\n", s.name, msg, keysAndValues)
} else {
fmt.Printf("[%s - ERROR] %s\n", s.name, msg)
}
_, args := s.formatter.FormatError(err, msg, keysAndValues)
fmt.Printf("[%s | ERROR] %s%s\n", s.name, msg, args)
}

func (s *sink) WithValues(keysAndValues ...interface{}) logr.LogSink {
Expand Down

0 comments on commit 2427a84

Please sign in to comment.