🎨 Allow to pass by a logger interface to context

This commit is contained in:
Ettore Di Giacinto 2022-05-26 13:06:47 +00:00 committed by GitHub
parent d6ae727d79
commit edd2275bf5
6 changed files with 19 additions and 15 deletions

View File

@ -30,7 +30,7 @@ import (
)
type Context struct {
*logger.Logger
types.Logger
context.Context
types.GarbageCollector
Config *types.LuetConfig
@ -122,13 +122,14 @@ func (c *Context) WithLoggingContext(name string) types.Context {
ctxCopy.Config = &configCopy
ctxCopy.annotations = ctx.annotations
ctxCopy.Logger, _ = c.Logger.Copy(logger.WithContext(name))
ctxCopy.Logger, _ = c.Logger.Copy()
ctxCopy.Logger.SetContext(name)
return ctxCopy
}
// Copy returns a context copy with a reset logging context
func (c *Context) Copy() types.Context {
func (c *Context) Clone() types.Context {
return c.WithLoggingContext("")
}

View File

@ -25,6 +25,7 @@ import (
log "github.com/ipfs/go-log/v2"
"github.com/kyokomi/emoji"
"github.com/mudler/luet/pkg/api/core/types"
"github.com/pterm/pterm"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@ -114,18 +115,19 @@ func New(opts ...LoggerOptions) (*Logger, error) {
return l, nil
}
func (l *Logger) Copy(opts ...LoggerOptions) (*Logger, error) {
// Copy returns a copy of the logger
func (l *Logger) Copy() (types.Logger, error) {
c := *l
copy := &c
for _, o := range opts {
if err := o(copy); err != nil {
return nil, err
}
}
return copy, nil
}
// SetContext sets the logger context, used to prefix log lines
func (l *Logger) SetContext(name string) {
l.context = name
}
func joinMsg(args ...interface{}) (message string) {
for _, m := range args {
message += " " + fmt.Sprintf("%v", m)

View File

@ -21,7 +21,6 @@ import (
"os"
"github.com/gookit/color"
"github.com/mudler/luet/pkg/api/core/logger"
. "github.com/mudler/luet/pkg/api/core/logger"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
@ -84,11 +83,12 @@ var _ = Describe("Context and logging", func() {
It("returns copies with logged context", func() {
l, err := New(WithLevel("debug"))
l, _ = l.Copy(logger.WithContext("bazzz"))
l2, _ := l.Copy()
l2.SetContext("bazzz")
Expect(err).ToNot(HaveOccurred())
Expect(captureStdout(func(w io.Writer) {
l.Debug("bar")
l2.Debug("bar")
})).To(ContainSubstring("(bazzz) bar"))
})

View File

@ -19,7 +19,7 @@ type Context interface {
Logger
GarbageCollector
GetConfig() LuetConfig
Copy() Context
Clone() Context
// SetAnnotation sets generic annotations to hold in a context
SetAnnotation(s string, i interface{})

View File

@ -33,7 +33,8 @@ type Logger interface {
Fatalf(string, ...interface{})
Panicf(string, ...interface{})
Tracef(string, ...interface{})
Copy() (Logger, error)
SetContext(string)
SpinnerStop()
Spinner()
Ask() bool

View File

@ -638,7 +638,7 @@ func (l *LuetInstaller) download(syncedRepos Repositories, toDownload map[string
var wg = new(sync.WaitGroup)
ctx := l.Options.Context.Copy()
ctx := l.Options.Context.Clone()
// Check if the terminal is big enough to display a progress bar
// https://github.com/pterm/pterm/blob/4c725e56bfd9eb38e1c7b9dec187b50b93baa8bd/progressbar_printer.go#L190