Migrate to pure slog without a custom wrapper

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/30616/head
Benoit Tigeot 2 years ago committed by Andrea Tartaglia
parent bde392a381
commit 77298e5abf
No known key found for this signature in database

@ -18,6 +18,7 @@ package monocular
import ( import (
"errors" "errors"
"log/slog"
"net/url" "net/url"
) )

@ -0,0 +1,43 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cli
import (
"log/slog"
"os"
)
func NewLogger(debug bool) *slog.Logger {
level := slog.LevelInfo
if debug {
level = slog.LevelDebug
}
// Create a handler that removes timestamps
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove the time attribute
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
})
return slog.New(handler)
}

@ -19,6 +19,7 @@ package cmd
import ( import (
"bytes" "bytes"
"io" "io"
"log/slog"
"os" "os"
"strings" "strings"
"testing" "testing"

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log/slog"
"net/http" "net/http"
"strings" "strings"
"sync" "sync"

@ -17,6 +17,8 @@ package kube // import "helm.sh/helm/v4/pkg/kube"
import ( import (
"context" "context"
"io"
"log/slog"
"testing" "testing"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"

@ -18,6 +18,8 @@ package kube // import "helm.sh/helm/v3/pkg/kube"
import ( import (
"errors" "errors"
"io"
"log/slog"
"testing" "testing"
"time" "time"

@ -151,9 +151,9 @@ func (hw *legacyWaiter) WaitForDelete(deleted ResourceList, timeout time.Duratio
elapsed := time.Since(startTime).Round(time.Second) elapsed := time.Since(startTime).Round(time.Second)
if err != nil { if err != nil {
slog.Debug("wait for resources failed", "elapsed", elapsed, slog.Any("error", err)) hw.log.Debug("wait for resources failed", "elapsed", elapsed, "error", err)
} else { } else {
slog.Debug("wait for resources succeeded", "elapsed", elapsed) hw.log.Debug("wait for resources succeeded", "elapsed", elapsed)
} }
return err return err
@ -397,7 +397,7 @@ func (hw *legacyWaiter) watchUntilReady(timeout time.Duration, info *resource.In
return nil return nil
} }
hw.log("Watching for changes to %s %s with timeout of %v", kind, info.Name, timeout) hw.log.Debug("watching for resource changes", "kind", kind, "resource", info.Name, "timeout", timeout)
// Use a selector on the name of the resource. This should be unique for the // Use a selector on the name of the resource. This should be unique for the
// given version and kind // given version and kind
@ -425,7 +425,8 @@ func (hw *legacyWaiter) watchUntilReady(timeout time.Duration, info *resource.In
// we get. We care mostly about jobs, where what we want to see is // we get. We care mostly about jobs, where what we want to see is
// the status go into a good state. For other types, like ReplicaSet // the status go into a good state. For other types, like ReplicaSet
// we don't really do anything to support these as hooks. // we don't really do anything to support these as hooks.
hw.log("Add/Modify event for %s: %v", info.Name, e.Type) hw.log.Debug("add/modify event received", "resource", info.Name, "eventType", e.Type)
switch kind { switch kind {
case "Job": case "Job":
return hw.waitForJob(obj, info.Name) return hw.waitForJob(obj, info.Name)
@ -434,11 +435,11 @@ func (hw *legacyWaiter) watchUntilReady(timeout time.Duration, info *resource.In
} }
return true, nil return true, nil
case watch.Deleted: case watch.Deleted:
hw.log("Deleted event for %s", info.Name) hw.log.Debug("deleted event received", "resource", info.Name)
return true, nil return true, nil
case watch.Error: case watch.Error:
// Handle error and return with an error. // Handle error and return with an error.
hw.log("Error event for %s", info.Name) hw.log.Error("error event received", "resource", info.Name)
return true, errors.Errorf("failed to deploy %s", info.Name) return true, errors.Errorf("failed to deploy %s", info.Name)
default: default:
return false, nil return false, nil
@ -464,7 +465,7 @@ func (hw *HelmWaiter) waitForJob(obj runtime.Object, name string) (bool, error)
} }
} }
hw.log("%s: Jobs active: %d, jobs failed: %d, jobs succeeded: %d", name, o.Status.Active, o.Status.Failed, o.Status.Succeeded) hw.log.Debug("job status update", "job", name, "active", o.Status.Active, "failed", o.Status.Failed, "succeeded", o.Status.Succeeded)
return false, nil return false, nil
} }

@ -19,6 +19,8 @@ package driver // import "helm.sh/helm/v4/pkg/storage/driver"
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"log/slog"
"testing" "testing"
sqlmock "github.com/DATA-DOG/go-sqlmock" sqlmock "github.com/DATA-DOG/go-sqlmock"

Loading…
Cancel
Save