Skip to content
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

Use consistent logging style #385

Merged
merged 1 commit into from
Jan 25, 2019
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
1 change: 1 addition & 0 deletions Gopkg.lock

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

33 changes: 23 additions & 10 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package builder
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"sort"
Expand All @@ -31,7 +32,7 @@ import (

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"
"github.com/sirupsen/logrus"
"github.com/apache/camel-k/pkg/util/log"
)

// ********************************
Expand All @@ -46,7 +47,7 @@ type buildTask struct {
}

type defaultBuilder struct {
log *logrus.Entry
log log.Logger
ctx context.Context
client client.Client
tasks chan buildTask
Expand All @@ -59,7 +60,7 @@ type defaultBuilder struct {
// New --
func New(ctx context.Context, c client.Client, namespace string) Builder {
m := defaultBuilder{
log: logrus.WithField("logger", "builder"),
log: log.WithName("builder"),
ctx: ctx,
client: c,
tasks: make(chan buildTask),
Expand Down Expand Up @@ -134,7 +135,18 @@ func (b *defaultBuilder) loop() {
func (b *defaultBuilder) process(request Request, handler func(*Result)) {
result, present := b.request.Load(request.Meta.Name)
if !present || result == nil {
b.log.Panicf("no info found for: %+v", request.Meta.Name)

r := result.(Result)
r.Status = StatusError
r.Error = fmt.Errorf("no info found for: %s/%s", request.Meta.Namespace, request.Meta.Name)

b.log.Error(r.Error, "error processing request")

if handler != nil {
handler(&r)
}

return
}

// update the status
Expand All @@ -153,7 +165,8 @@ func (b *defaultBuilder) process(request Request, handler func(*Result)) {
}
builderPath, err := ioutil.TempDir(buildDir, "builder-")
if err != nil {
logrus.Warning("Unexpected error while creating a temporary dir ", err)
log.Error(err, "Unexpected error while creating a temporary dir")

r.Status = StatusError
r.Error = err
}
Expand Down Expand Up @@ -216,11 +229,11 @@ func (b *defaultBuilder) process(request Request, handler func(*Result)) {
case <-request.C.Done():
r.Status = StatusInterrupted
default:
l := b.log.WithFields(logrus.Fields{
"step": step.ID(),
"phase": step.Phase(),
"context": request.Meta.Name,
})
l := b.log.WithValues(
"step", step.ID(),
"phase", step.Phase(),
"context", request.Meta.Name,
)

l.Infof("executing step")

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/log"
k8slog "github.com/apache/camel-k/pkg/util/kubernetes/log"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (o *logCmdOptions) run(cmd *cobra.Command, args []string) error {
if err := c.Get(o.Context, key, &integration); err != nil {
return err
}
if err := log.Print(o.Context, c, &integration); err != nil {
if err := k8slog.Print(o.Context, c, &integration); err != nil {
return err
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ import (
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/log"
k8slog "github.com/apache/camel-k/pkg/util/kubernetes/log"
"github.com/apache/camel-k/pkg/util/sync"
"github.com/apache/camel-k/pkg/util/watch"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -192,7 +191,7 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
}
}
if o.Logs || o.Dev {
err = log.Print(o.Context, c, integration)
err = k8slog.Print(o.Context, c, integration)
if err != nil {
return err
}
Expand Down Expand Up @@ -244,7 +243,7 @@ func (o *runCmdOptions) syncIntegration(c client.Client, sources []string) error
case <-changes:
_, err := o.updateIntegrationCode(c, sources)
if err != nil {
logrus.Error("Unable to sync integration: ", err)
fmt.Println("Unable to sync integration: ", err.Error())
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/controller/integration/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/util/log"
)

// Action --
Expand All @@ -36,12 +37,20 @@ type Action interface {

// executes the handling function
Handle(ctx context.Context, integration *v1alpha1.Integration) error

// Inject integration logger
InjectLogger(log.Logger)
}

type baseAction struct {
client client.Client
L log.Logger
}

func (action *baseAction) InjectClient(client client.Client) {
action.client = client
}

func (action *baseAction) InjectLogger(log log.Logger) {
action.L = log
}
5 changes: 2 additions & 3 deletions pkg/controller/integration/build_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/digest"
"github.com/rs/xid"
"github.com/sirupsen/logrus"
)

// NewBuildContextAction create an action that handles integration context build
Expand Down Expand Up @@ -81,7 +80,7 @@ func (action *buildContextAction) Handle(ctx context.Context, integration *v1alp
return err
}

logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase)
action.L.Info("Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
}
Expand All @@ -102,7 +101,7 @@ func (action *buildContextAction) Handle(ctx context.Context, integration *v1alp
return err
}

logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase)
action.L.Info("Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/controller/integration/build_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/digest"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/sirupsen/logrus"

corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -80,7 +78,7 @@ func (action *buildImageAction) handleBuildImageRunning(ctx context.Context, int
}

if b.IsBuilding(integration.ObjectMeta) {
logrus.Infof("Build for integration %s is running", integration.Name)
action.L.Info("Build running")
}

return nil
Expand Down Expand Up @@ -139,7 +137,7 @@ func (action *buildImageAction) handleBuildImageSubmitted(ctx context.Context, i
// this function is invoked synchronously for every state change
//
if err := action.handleBuildStateChange(result.Request.C, result); err != nil {
logrus.Warnf("Error while building integration image %s, reason: %s", ictx.Name, err.Error())
action.L.Error(err, "Error while building integration image")
}
})
}
Expand All @@ -158,11 +156,11 @@ func (action *buildImageAction) handleBuildStateChange(ctx context.Context, res

switch res.Status {
case builder.StatusSubmitted:
logrus.Info("Build submitted")
action.L.Info("Build submitted")
case builder.StatusStarted:
target.Status.Phase = v1alpha1.IntegrationPhaseBuildImageRunning

logrus.Infof("Integration %s transitioning to state %s", target.Name, target.Status.Phase)
action.L.Info("Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
case builder.StatusError:
Expand All @@ -179,7 +177,7 @@ func (action *buildImageAction) handleBuildStateChange(ctx context.Context, res
}
}

logrus.Infof("Integration %s transitioning to state %s, reason: %s", target.Name, target.Status.Phase, res.Error.Error())
action.L.Error(res.Error, "Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
case builder.StatusCompleted:
Expand All @@ -197,7 +195,7 @@ func (action *buildImageAction) handleBuildStateChange(ctx context.Context, res

target.Status.Digest = dgst

logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase)
action.L.Info("Integration state transition", "phase", target.Status.Phase)

if err := action.client.Status().Update(ctx, target); err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/integration/build_image_failure_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/platform"
"github.com/sirupsen/logrus"
)

// NewErrorRecoveryAction creates a new error recovering handling action for the integration
Expand Down Expand Up @@ -58,15 +57,15 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, integration *v1al
// The integration platform needs to be initialized before starting handle
// context in status error
if _, err := platform.GetCurrentPlatform(ctx, action.client, integration.Namespace); err != nil {
logrus.Info("Waiting for a integration platform to be initialized")
action.L.Info("Waiting for a integration platform to be initialized")
return nil
}

if integration.Status.Failure.Recovery.Attempt > integration.Status.Failure.Recovery.AttemptMax {
target := integration.DeepCopy()
target.Status.Phase = v1alpha1.IntegrationPhaseError

logrus.Infof("Max recovery attempt reached for integration %s, transition to phase %s", integration.Name, string(target.Status.Phase))
action.L.Info("Max recovery attempt reached, transition to error phase")

return action.client.Status().Update(ctx, target)
}
Expand All @@ -91,7 +90,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, integration *v1al
target.Status.Failure.Recovery.Attempt = integration.Status.Failure.Recovery.Attempt + 1
target.Status.Failure.Recovery.AttemptTime = time.Now()

logrus.Infof("Recovery attempt for integration %s (%d/%d)",
action.L.Info("Recovery attempt (%d/%d)",
integration.Name,
target.Status.Failure.Recovery.Attempt,
target.Status.Failure.Recovery.AttemptMax,
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/integration/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -73,7 +72,8 @@ func (action *deployAction) Handle(ctx context.Context, integration *v1alpha1.In

target := integration.DeepCopy()
target.Status.Phase = v1alpha1.IntegrationPhaseRunning
logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase)

action.L.Info("Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
}
9 changes: 4 additions & 5 deletions pkg/controller/integration/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import (
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/digest"
"github.com/sirupsen/logrus"
)

// NewInitializeAction creates a new inititialize action
// NewInitializeAction creates a new initialize action
func NewInitializeAction() Action {
return &initializeAction{}
}
Expand All @@ -54,13 +53,13 @@ func (action *initializeAction) Handle(ctx context.Context, integration *v1alpha

// The integration platform needs to be ready before starting to create integrations
if err != nil || pl.Status.Phase != v1alpha1.IntegrationPlatformPhaseReady {
logrus.Info("Waiting for a integration platform to be ready")
action.L.Info("Waiting for the integration platform to be initialized")

if integration.Status.Phase != v1alpha1.IntegrationPhaseWaitingForPlatform {
target := integration.DeepCopy()
target.Status.Phase = v1alpha1.IntegrationPhaseWaitingForPlatform

logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase)
action.L.Info("Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
}
Expand Down Expand Up @@ -107,7 +106,7 @@ func (action *initializeAction) Handle(ctx context.Context, integration *v1alpha
target.Status.Context = integration.Spec.Context
target.Status.Image = ""

logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase)
action.L.Info("Integration state transition", "phase", target.Status.Phase)

return action.client.Status().Update(ctx, target)
}
Loading