New logger for the Traefik logs

This commit is contained in:
Ludovic Fernandez 2022-11-21 18:36:05 +01:00 committed by GitHub
parent 27c02b5a56
commit 56f7515ecd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
297 changed files with 2337 additions and 1934 deletions

View file

@ -6,8 +6,9 @@ import (
"net/http"
"os"
"github.com/sirupsen/logrus"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v2/pkg/logs"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
@ -35,12 +36,13 @@ func NewBuilder(client *Client, plugins map[string]Descriptor, localPlugins map[
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
}
logger := log.WithoutContext().WithFields(logrus.Fields{"plugin": "plugin-" + pName, "module": desc.ModuleName})
logger := log.With().Str("plugin", "plugin-"+pName).Str("module", desc.ModuleName).Logger()
i := interp.New(interp.Options{
GoPath: client.GoPath(),
Env: os.Environ(),
Stdout: logger.WriterLevel(logrus.DebugLevel),
Stderr: logger.WriterLevel(logrus.ErrorLevel),
Stdout: logs.NoLevel(logger, zerolog.DebugLevel),
Stderr: logs.NoLevel(logger, zerolog.ErrorLevel),
})
err = i.Use(stdlib.Symbols)
@ -83,12 +85,13 @@ func NewBuilder(client *Client, plugins map[string]Descriptor, localPlugins map[
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
}
logger := log.WithoutContext().WithFields(logrus.Fields{"plugin": "plugin-" + pName, "module": desc.ModuleName})
logger := log.With().Str("plugin", "plugin-"+pName).Str("module", desc.ModuleName).Logger()
i := interp.New(interp.Options{
GoPath: localGoPath,
Env: os.Environ(),
Stdout: logger.WriterLevel(logrus.DebugLevel),
Stderr: logger.WriterLevel(logrus.ErrorLevel),
Stdout: logs.NoLevel(logger, zerolog.DebugLevel),
Stderr: logs.NoLevel(logger, zerolog.ErrorLevel),
})
err = i.Use(stdlib.Symbols)

View file

@ -7,7 +7,7 @@ import (
"strings"
"github.com/hashicorp/go-multierror"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/rs/zerolog/log"
)
const localGoPath = "./plugins-local/"
@ -27,7 +27,7 @@ func SetupRemotePlugins(client *Client, plugins map[string]Descriptor) error {
ctx := context.Background()
for pAlias, desc := range plugins {
log.FromContext(ctx).Debugf("loading of plugin: %s: %s@%s", pAlias, desc.ModuleName, desc.Version)
log.Ctx(ctx).Debug().Msgf("Loading of plugin: %s: %s@%s", pAlias, desc.ModuleName, desc.Version)
hash, err := client.Download(ctx, desc.ModuleName, desc.Version)
if err != nil {

View file

@ -9,8 +9,9 @@ import (
"strings"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/logs"
"github.com/traefik/traefik/v2/pkg/provider"
"github.com/traefik/traefik/v2/pkg/safe"
"github.com/traefik/yaegi/interp"
@ -158,21 +159,21 @@ func (p *Provider) Init() error {
func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
defer func() {
if err := recover(); err != nil {
log.WithoutContext().WithField(log.ProviderName, p.name).Errorf("panic inside the plugin %v", err)
log.Error().Str(logs.ProviderName, p.name).Msgf("Panic inside the plugin %v", err)
}
}()
cfgChan := make(chan json.Marshaler)
pool.GoCtx(func(ctx context.Context) {
logger := log.FromContext(log.With(ctx, log.Str(log.ProviderName, p.name)))
logger := log.Ctx(ctx).With().Str(logs.ProviderName, p.name).Logger()
for {
select {
case <-ctx.Done():
err := p.pp.Stop()
if err != nil {
logger.Errorf("failed to stop the provider: %v", err)
logger.Error().Err(err).Msg("Failed to stop the provider")
}
return
@ -180,14 +181,14 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
case cfgPg := <-cfgChan:
marshalJSON, err := cfgPg.MarshalJSON()
if err != nil {
logger.Errorf("failed to marshal configuration: %v", err)
logger.Error().Err(err).Msg("Failed to marshal configuration")
continue
}
cfg := &dynamic.Configuration{}
err = json.Unmarshal(marshalJSON, cfg)
if err != nil {
logger.Errorf("failed to unmarshal configuration: %v", err)
logger.Error().Err(err).Msg("Failed to unmarshal configuration")
continue
}