Skip to content
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
71 changes: 71 additions & 0 deletions mocks/pkg/types/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions mocks/pkg/types/trace_exiter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions mocks/pkg/types/tracer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions pkg/runtime/log/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 (
"context"

acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
)

const (
// ContextKey is the string key used to store a logger in a Context
ContextKey = "ack.logger"
)

// FromContext returns a `pkg/types.Logger` from a saved key in the request
// context
func FromContext(ctx context.Context) acktypes.Logger {
if v := ctx.Value(ContextKey); v != nil {
return v.(*ResourceLogger)
}
return NoopLogger
}
Comment on lines +22 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! 👍

39 changes: 39 additions & 0 deletions pkg/runtime/log/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 (
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
)

var (
// NoopLogger is useful for testing/mocking
NoopLogger acktypes.Logger = &voidLogger{}
)

// voidLogger implements Logger but does nothing. Useful for
// testing and mocking...
type voidLogger struct{}

func (l *voidLogger) WithValues(...interface{}) {}
func (l *voidLogger) Info(string, ...interface{}) {}
func (l *voidLogger) Debug(string, ...interface{}) {}
func (l *voidLogger) Trace(name string, additionalValues ...interface{}) acktypes.TraceExiter {
f := func(err error, args ...interface{}) {
l.Exit(name, err, args...)
}
return f
}
func (l *voidLogger) Enter(string, ...interface{}) {}
func (l *voidLogger) Exit(string, error, ...interface{}) {}
94 changes: 94 additions & 0 deletions pkg/runtime/log/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,106 @@
package log

import (
"strings"

"github.com/go-logr/logr"

"github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
)

// ResourceLogger is a wrapper around a logr.Logger that writes log messages
// about resources involved in a controller loop. It implements
// `pkg/types.Logger`
type ResourceLogger struct {
log logr.Logger
res acktypes.AWSResource
blockDepth int
}

// WithValues adapts the internal logger with a set of additional values
func (rl *ResourceLogger) WithValues(
values ...interface{},
) {
rl.log = rl.log.WithValues(values...)
}

// Debug writes a supplied log message about a resource that includes a set of
// standard log values for the resource's kind, namespace, name, etc
func (rl *ResourceLogger) Debug(
msg string,
additionalValues ...interface{},
) {
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
}

// Info writes a supplied log message about a resource that includes a
// set of standard log values for the resource's kind, namespace, name, etc
func (rl *ResourceLogger) Info(
msg string,
additionalValues ...interface{},
) {
AdaptResource(rl.log, rl.res, additionalValues...).V(0).Info(msg)
}

// Enter logs an entry to a function or code block
func (rl *ResourceLogger) Enter(
name string, // name of the function or code block we're entering
additionalValues ...interface{},
) {
if rl.log.V(1).Enabled() {
rl.blockDepth++
depth := strings.Repeat(">", rl.blockDepth)
msg := depth + " " + name
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
}
}

// Exit logs an exit from a function or code block
func (rl *ResourceLogger) Exit(
name string, // name of the function or code block we're exiting
err error,
additionalValues ...interface{},
) {
if rl.log.V(1).Enabled() {
depth := strings.Repeat("<", rl.blockDepth)
msg := depth + " " + name
if err != nil {
additionalValues = append(additionalValues, "error")
additionalValues = append(additionalValues, err)
}
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
rl.blockDepth--
}
}

// Trace logs an entry to a function or code block and returns a functor
// that can be called to log the exit of the function or code block
func (rl *ResourceLogger) Trace(
name string,
additionalValues ...interface{},
) acktypes.TraceExiter {
rl.Enter(name, additionalValues...)
f := func(err error, args ...interface{}) {
rl.Exit(name, err, args...)
}
return f
}

// NewResourceLogger returns a resourceLogger that can write log messages about
// a resource.
func NewResourceLogger(
log logr.Logger,
res acktypes.AWSResource,
additionalValues ...interface{},
) *ResourceLogger {
return &ResourceLogger{
log: AdaptResource(log, res, additionalValues...),
res: res,
blockDepth: 0,
}
}

// AdaptResource returns a logger with log values set for the resource's kind,
// namespace, name, etc
func AdaptResource(
Expand Down
Loading