Skip to content

Commit

Permalink
refactor: Logger initializes logging (#867)
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
Terry Howe authored Mar 21, 2023
1 parent b58e7b9 commit 660d167
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 41 deletions.
3 changes: 2 additions & 1 deletion cmd/oras/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"oras.land/oras-go/v2/content/file"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/graph"
"oras.land/oras/internal/trace"
)

type attachOptions struct {
Expand Down Expand Up @@ -98,7 +99,7 @@ Example - Attach file to the manifest tagged 'v1' in an OCI layout folder 'layou
}

func runAttach(opts attachOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
annotations, err := opts.LoadManifestAnnotations()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/blob/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"oras.land/oras-go/v2/errdef"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type deleteBlobOptions struct {
Expand Down Expand Up @@ -70,7 +71,7 @@ Example - Delete a blob and print its descriptor:
}

func deleteBlob(opts deleteBlobOptions) (err error) {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/blob/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type fetchBlobOptions struct {
Expand Down Expand Up @@ -89,7 +90,7 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar'
}

func fetchBlob(opts fetchBlobOptions) (fetchErr error) {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
var target oras.ReadOnlyTarget
target, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/file"
"oras.land/oras/internal/trace"
)

type pushBlobOptions struct {
Expand Down Expand Up @@ -95,7 +96,7 @@ Example - Push blob 'hi.txt' into an OCI layout folder 'layout-dir':
}

func pushBlob(opts pushBlobOptions) (err error) {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)

repo, err := opts.NewTarget(opts.Common)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/graph"
"oras.land/oras/internal/trace"
)

type copyOptions struct {
Expand Down Expand Up @@ -96,7 +97,7 @@ Example - Copy an artifact with multiple tags with concurrency tuned:
}

func runCopy(opts copyOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)

// Prepare source
src, err := opts.From.NewReadonlyTarget(ctx, opts.Common)
Expand Down
13 changes: 7 additions & 6 deletions cmd/oras/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import (
"os"
"strings"

"gopkg.in/yaml.v3"
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/graph"

"github.com/need-being/go-tree"
"github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/graph"
"oras.land/oras/internal/trace"
)

type discoverOptions struct {
Expand Down Expand Up @@ -91,7 +92,7 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI layout folder
}

func runDiscover(opts discoverOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
repo, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
return err
Expand Down
17 changes: 0 additions & 17 deletions cmd/oras/internal/option/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ limitations under the License.
package option

import (
"context"

"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"oras.land/oras/internal/trace"
)

// Common option struct.
Expand All @@ -34,16 +30,3 @@ func (opts *Common) ApplyFlags(fs *pflag.FlagSet) {
fs.BoolVarP(&opts.Debug, "debug", "d", false, "debug mode")
fs.BoolVarP(&opts.Verbose, "verbose", "v", false, "verbose output")
}

// SetLoggerLevel sets up the logger based on common options.
func (opts *Common) SetLoggerLevel() (context.Context, logrus.FieldLogger) {
var logLevel logrus.Level
if opts.Debug {
logLevel = logrus.DebugLevel
} else if opts.Verbose {
logLevel = logrus.InfoLevel
} else {
logLevel = logrus.WarnLevel
}
return trace.WithLoggerLevel(context.Background(), logLevel)
}
3 changes: 2 additions & 1 deletion cmd/oras/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/credential"
"oras.land/oras/internal/trace"
)

type loginOptions struct {
Expand Down Expand Up @@ -73,7 +74,7 @@ Example - Log in with username and password in an interactive terminal and no TL
}

func runLogin(opts loginOptions) (err error) {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)

// prompt for credential
if opts.Password == "" {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/manifest/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"oras.land/oras-go/v2/errdef"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type deleteOptions struct {
Expand Down Expand Up @@ -75,7 +76,7 @@ Example - Delete a manifest by digest 'sha256:99e4703fbf30916f549cd6bfa9cdbab614
}

func deleteManifest(opts deleteOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type fetchOptions struct {
Expand Down Expand Up @@ -89,7 +90,7 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar':
}

func fetchManifest(opts fetchOptions) (fetchErr error) {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)

target, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/manifest/fetch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"oras.land/oras-go/v2/content"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/descriptor"
"oras.land/oras/internal/trace"
)

type fetchConfigOptions struct {
Expand Down Expand Up @@ -86,7 +87,7 @@ Example - Fetch and print the prettified descriptor of the config:
}

func fetchConfig(opts fetchConfigOptions) (fetchErr error) {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)

repo, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/file"
"oras.land/oras/internal/trace"
)

type pushOptions struct {
Expand Down Expand Up @@ -104,7 +105,7 @@ Example - Push a manifest to an OCI layout folder 'layout-dir' and tag with 'v1'
}

func pushManifest(opts pushOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
var target oras.Target
var err error
target, err = opts.NewTarget(opts.Common)
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"oras.land/oras/cmd/oras/internal/fileref"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/graph"
"oras.land/oras/internal/trace"
)

type pullOptions struct {
Expand Down Expand Up @@ -183,7 +184,7 @@ func runPull(opts pullOptions) error {
return ret, nil
}

ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
target, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/fileref"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type pushOptions struct {
Expand Down Expand Up @@ -123,7 +124,7 @@ Example - Push file "hi.txt" into an OCI layout folder 'layout-dir' with tag 'te
}

func runPush(opts pushOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
annotations, err := opts.LoadManifestAnnotations()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/repo/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/repository"
"oras.land/oras/internal/trace"
)

type repositoryOptions struct {
Expand Down Expand Up @@ -68,7 +69,7 @@ Example - List the repositories under the registry that include values lexically
}

func listRepository(opts repositoryOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/repo/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/opencontainers/go-digest"
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type showTagsOptions struct {
Expand Down Expand Up @@ -77,7 +78,7 @@ Example - Show tags associated with a digest:
}

func showTags(opts showTagsOptions) error {
ctx, logger := opts.SetLoggerLevel()
ctx, logger := trace.NewLogger(opts.Debug, opts.Verbose)
finder, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/trace"
)

type tagOptions struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ Example - Tag the manifest 'v1.0.1' to 'v1.0.2' in an OCI layout folder 'layout-
}

func tagManifest(opts tagOptions) error {
ctx, _ := opts.SetLoggerLevel()
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
target, err := opts.NewTarget(opts.Common)
if err != nil {
return err
Expand Down
16 changes: 13 additions & 3 deletions internal/trace/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ type contextKey int
// loggerKey is the associated key type for logger entry in context.
const loggerKey contextKey = iota

// WithLoggerLevel returns a context with logrus log entry.
func WithLoggerLevel(ctx context.Context, level logrus.Level) (context.Context, logrus.FieldLogger) {
// NewLogger returns a logger.
func NewLogger(debug bool, verbose bool) (context.Context, logrus.FieldLogger) {
var logLevel logrus.Level
if debug {
logLevel = logrus.DebugLevel
} else if verbose {
logLevel = logrus.InfoLevel
} else {
logLevel = logrus.WarnLevel
}

logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{DisableQuote: true})
logger.SetLevel(level)
logger.SetLevel(logLevel)
ctx := context.Background()
entry := logger.WithContext(ctx)
return context.WithValue(ctx, loggerKey, entry), entry
}
Expand Down

0 comments on commit 660d167

Please sign in to comment.