Skip to content

Commit

Permalink
Discard logger (#80)
Browse files Browse the repository at this point in the history
Discard logger by default unless `--debug` is toggled.
  • Loading branch information
shizhMSFT committed Apr 18, 2019
1 parent 550ed34 commit d1e0198
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cmd/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/deislabs/oras/pkg/content"
ctxo "github.com/deislabs/oras/pkg/context"
"github.com/deislabs/oras/pkg/oras"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -64,8 +65,11 @@ Example - Pull all files, any media type:
}

func runPull(opts pullOptions) error {
ctx := context.Background()
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
ctx = ctxo.WithLoggerDiscarded(ctx)
}
if opts.allowAllMediaTypes {
opts.allowedMediaTypes = nil
Expand All @@ -78,7 +82,7 @@ func runPull(opts pullOptions) error {
defer store.Close()
store.DisableOverwrite = opts.keepOldFiles
store.AllowPathTraversalOnWrite = opts.pathTraversal
desc, files, err := oras.Pull(context.Background(), resolver, opts.targetRef, store, oras.WithAllowedMediaTypes(opts.allowedMediaTypes))
desc, files, err := oras.Pull(ctx, resolver, opts.targetRef, store, oras.WithAllowedMediaTypes(opts.allowedMediaTypes))
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/deislabs/oras/pkg/content"
ctxo "github.com/deislabs/oras/pkg/context"
"github.com/deislabs/oras/pkg/oras"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -73,8 +74,11 @@ Example - Push file "hi.txt" with the custom manifest config "config.json" of th
}

func runPush(opts pushOptions) error {
ctx := context.Background()
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
ctx = ctxo.WithLoggerDiscarded(ctx)
}

// load files
Expand Down Expand Up @@ -138,7 +142,7 @@ func runPush(opts pushOptions) error {

// ready to push
resolver := newResolver(opts.username, opts.password, opts.configs...)
desc, err := oras.Push(context.Background(), resolver, opts.targetRef, store, files, pushOpts...)
desc, err := oras.Push(ctx, resolver, opts.targetRef, store, files, pushOpts...)
if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package context

import "context"

// Background returns a default context with logger discarded.
func Background() context.Context {
ctx := context.Background()
return WithLoggerDiscarded(ctx)
}
35 changes: 35 additions & 0 deletions pkg/context/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package context

import (
"context"
"io"
"io/ioutil"

"github.com/containerd/containerd/log"
"github.com/sirupsen/logrus"
)

// WithLogger returns a new context with the provided logger.
// This method wraps github.com/containerd/containerd/log.WithLogger()
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
return log.WithLogger(ctx, logger)
}

// WithLoggerFromWriter returns a new context with the logger, writting to the provided logger.
func WithLoggerFromWriter(ctx context.Context, writer io.Writer) context.Context {
logger := logrus.New()
logger.Out = writer
entry := logrus.NewEntry(logger)
return WithLogger(ctx, entry)
}

// WithLoggerDiscarded returns a new context with the logger, writting to nothing.
func WithLoggerDiscarded(ctx context.Context) context.Context {
return WithLoggerFromWriter(ctx, ioutil.Discard)
}

// GetLogger retrieves the current logger from the context.
// This method wraps github.com/containerd/containerd/log.GetLogger()
func GetLogger(ctx context.Context) *logrus.Entry {
return log.GetLogger(ctx)
}

0 comments on commit d1e0198

Please sign in to comment.