Skip to content

Filter error log #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
workloadv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2"
"github.com/project-codeflare/appwrapper/pkg/config"
"github.com/project-codeflare/appwrapper/pkg/controller"
"github.com/project-codeflare/appwrapper/pkg/logger"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func main() {
opts.BindFlags(flag.CommandLine)
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
ctrl.SetLogger(logger.FilteredLogger(zap.New(zap.UseFlagOptions(&opts))))
setupLog.Info("Build info", "version", BuildVersion, "date", BuildDate)

namespace, err := getNamespace()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.4

require (
github.com/distribution/reference v0.5.0
github.com/go-logr/logr v1.4.2
github.com/golangci/golangci-lint v1.55.2
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
Expand All @@ -29,7 +30,6 @@ require (
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down
61 changes: 61 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2024 IBM Corporation.

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 logger

import (
"github.com/go-logr/logr"

"k8s.io/apimachinery/pkg/api/errors"
)

// logSink implements a filtered log sink
type logSink struct {
sink logr.LogSink
}

func (l logSink) Init(info logr.RuntimeInfo) {
l.sink.Init(info)
}

func (l logSink) Enabled(level int) bool {
return l.sink.Enabled(level)
}
func (l logSink) Info(level int, msg string, keysAndValues ...any) {
l.sink.Info(level, msg, keysAndValues...)
}

func (l logSink) Error(err error, msg string, keysAndValues ...any) {
// replace StatusReasonConflict and StatusReasonAlreadyExists errors with debug messages
if errors.IsConflict(err) || errors.IsAlreadyExists(err) {
l.sink.Info(1, msg, append(keysAndValues, "error", err.Error())...)
} else {
l.sink.Error(err, msg, keysAndValues...)
}
}

func (l logSink) WithValues(keysAndValues ...any) logr.LogSink {
return logSink{l.sink.WithValues(keysAndValues...)}
}

func (l logSink) WithName(name string) logr.LogSink {
return logSink{l.sink.WithName(name)}
}

// FilteredLogger returns a copy of the logger with a filtered sink
func FilteredLogger(logger logr.Logger) logr.Logger {
return logger.WithSink(logSink{logger.GetSink()})
}