mirror of https://github.com/helm/helm
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>pull/30708/head
parent
947658a96e
commit
b2380720eb
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
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 log
|
|
||||||
|
|
||||||
// Logger defines a minimal logging interface compatible with structured logging.
|
|
||||||
// It provides methods for different log levels with structured key-value pairs.
|
|
||||||
type Logger interface {
|
|
||||||
// Debug logs a message at debug level with structured key-value pairs.
|
|
||||||
Debug(msg string, args ...any)
|
|
||||||
|
|
||||||
// Warn logs a message at warning level with structured key-value pairs.
|
|
||||||
Warn(msg string, args ...any)
|
|
||||||
|
|
||||||
// Error logs a message at error level with structured key-value pairs.
|
|
||||||
Error(msg string, args ...any)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NopLogger is a logger implementation that discards all log messages.
|
|
||||||
type NopLogger struct{}
|
|
||||||
|
|
||||||
// Debug implements Logger.Debug by doing nothing.
|
|
||||||
func (NopLogger) Debug(_ string, _ ...any) {}
|
|
||||||
|
|
||||||
// Warn implements Logger.Warn by doing nothing.
|
|
||||||
func (NopLogger) Warn(_ string, _ ...any) {}
|
|
||||||
|
|
||||||
// Error implements Logger.Error by doing nothing.
|
|
||||||
func (NopLogger) Error(_ string, _ ...any) {}
|
|
||||||
|
|
||||||
// DefaultLogger provides a no-op logger that discards all messages.
|
|
||||||
// It can be used as a default when no logger is provided.
|
|
||||||
var DefaultLogger Logger = NopLogger{}
|
|
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
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 log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SlogAdapter adapts a standard library slog.Logger to the Logger interface.
|
|
||||||
type SlogAdapter struct {
|
|
||||||
logger *slog.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug implements Logger.Debug by forwarding to the underlying slog.Logger.
|
|
||||||
func (a SlogAdapter) Debug(msg string, args ...any) {
|
|
||||||
a.logger.Debug(msg, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warn implements Logger.Warn by forwarding to the underlying slog.Logger.
|
|
||||||
func (a SlogAdapter) Warn(msg string, args ...any) {
|
|
||||||
a.logger.Warn(msg, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error implements Logger.Error by forwarding to the underlying slog.Logger.
|
|
||||||
func (a SlogAdapter) Error(msg string, args ...any) {
|
|
||||||
// TODO: Handle error with `slog.Any`: slog.Info("something went wrong", slog.Any("err", err))
|
|
||||||
a.logger.Error(msg, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSlogAdapter creates a Logger that forwards log messages to a slog.Logger.
|
|
||||||
func NewSlogAdapter(logger *slog.Logger) Logger {
|
|
||||||
if logger == nil {
|
|
||||||
return DefaultLogger
|
|
||||||
}
|
|
||||||
return SlogAdapter{logger: logger}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewReadableTextLogger creates a Logger that outputs in a readable text format without timestamps
|
|
||||||
func NewReadableTextLogger(output io.Writer, debugEnabled bool) Logger {
|
|
||||||
level := slog.LevelInfo
|
|
||||||
if debugEnabled {
|
|
||||||
level = slog.LevelDebug
|
|
||||||
}
|
|
||||||
|
|
||||||
handler := slog.NewTextHandler(output, &slog.HandlerOptions{
|
|
||||||
Level: level,
|
|
||||||
// Ignore the time key to avoid cluttering the output with timestamps
|
|
||||||
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
|
|
||||||
if a.Key == slog.TimeKey {
|
|
||||||
return slog.Attr{}
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return NewSlogAdapter(slog.New(handler))
|
|
||||||
}
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in new issue