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

disable aws sdk logger if log level is not "trace" #460

Merged
merged 5 commits into from
Jul 18, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## not released yet

#### Improvements
- Disable AWS SDK logger if log level is not "trace"

## v2.0.0 - 4 Jul 2022

Expand Down
1 change: 1 addition & 0 deletions command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func NewStorageOpts(c *cli.Context) storage.Options {
NoVerifySSL: c.Bool("no-verify-ssl"),
RequestPayer: c.String("request-payer"),
UseListObjectsV1: c.Bool("use-list-objects-v1"),
LogLevel: log.LevelFromString(c.String("log")),
}
}

Expand Down
54 changes: 27 additions & 27 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ func Init(level string, json bool) {

// Trace prints message in trace mode.
func Trace(msg Message) {
global.printf(levelTrace, msg, os.Stdout)
global.printf(LevelTrace, msg, os.Stdout)
}

// Debug prints message in debug mode.
func Debug(msg Message) {
global.printf(levelDebug, msg, os.Stdout)
global.printf(LevelDebug, msg, os.Stdout)
}

// Info prints message in info mode.
func Info(msg Message) {
global.printf(levelInfo, msg, os.Stdout)
global.printf(LevelInfo, msg, os.Stdout)
}

// Stat prints stat message regardless of the log level with info print formatting.
// It uses printfHelper instead of printf to ignore the log level condition.
func Stat(msg Message) {
global.printfHelper(levelInfo, msg, os.Stdout)
global.printfHelper(LevelInfo, msg, os.Stdout)
}

// Error prints message in error mode.
func Error(msg Message) {
global.printf(levelError, msg, os.Stderr)
global.printf(LevelError, msg, os.Stderr)
}

// Close closes logger and its channel.
Expand All @@ -58,12 +58,12 @@ func Close() {
type Logger struct {
donech chan struct{}
json bool
level logLevel
level LogLevel
}

// New creates new logger.
func New(level string, json bool) *Logger {
logLevel := levelFromString(level)
logLevel := LevelFromString(level)
logger := &Logger{
donech: make(chan struct{}),
json: json,
Expand All @@ -74,14 +74,14 @@ func New(level string, json bool) *Logger {
}

// printf prints message according to the given level, message and std mode.
func (l *Logger) printf(level logLevel, message Message, std *os.File) {
func (l *Logger) printf(level LogLevel, message Message, std *os.File) {
if level < l.level {
return
}
l.printfHelper(level, message, std)
}

func (l *Logger) printfHelper(level logLevel, message Message, std *os.File) {
func (l *Logger) printfHelper(level LogLevel, message Message, std *os.File) {
if l.json {
outputCh <- output{
message: message.JSON(),
Expand All @@ -104,26 +104,26 @@ func (l *Logger) out() {
}
}

// logLevel is the level of Logger.
type logLevel int
// LogLevel is the level of Logger.
type LogLevel int

const (
levelTrace logLevel = iota
levelDebug
levelInfo
levelError
LevelTrace LogLevel = iota
LevelDebug
LevelInfo
LevelError
)

// String returns the string representation of logLevel.
func (l logLevel) String() string {
func (l LogLevel) String() string {
switch l {
case levelInfo:
case LevelInfo:
return ""
case levelError:
case LevelError:
return "ERROR "
case levelDebug:
case LevelDebug:
return "DEBUG "
case levelTrace:
case LevelTrace:
// levelTrace is used for printing aws sdk logs and
// aws-sdk-go already adds "DEBUG" prefix to logs.
// So do not add another prefix to log which makes it
Expand All @@ -134,19 +134,19 @@ func (l logLevel) String() string {
}
}

// levelFromString returns logLevel for given string. It
// LevelFromString returns logLevel for given string. It
// return `levelInfo` as a default.
func levelFromString(s string) logLevel {
func LevelFromString(s string) LogLevel {
switch s {
case "debug":
return levelDebug
return LevelDebug
case "info":
return levelInfo
return LevelInfo
case "error":
return levelError
return LevelError
case "trace":
return levelTrace
return LevelTrace
default:
return levelInfo
return LevelInfo
}
}
9 changes: 6 additions & 3 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,12 @@ func (sc *SessionCache) newSession(ctx context.Context, opts Options) (*session.
WithEndpoint(endpointURL.String()).
WithS3ForcePathStyle(!isVirtualHostStyle).
WithS3UseAccelerate(useAccelerate).
WithHTTPClient(httpClient).
WithLogLevel(aws.LogDebug).
WithLogger(sdkLogger{})
WithHTTPClient(httpClient)

if opts.LogLevel == log.LevelTrace {
awsCfg = awsCfg.WithLogLevel(aws.LogDebug).
WithLogger(sdkLogger{})
}

awsCfg.Retryer = newCustomRetryer(opts.MaxRetries)

Expand Down
46 changes: 46 additions & 0 deletions storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,52 @@ func TestS3ListObjectsAPIVersions(t *testing.T) {
})
}

func TestAWSLogLevel(t *testing.T) {
testcases := []struct {
name string
level string
expected aws.LogLevelType
}{
{
name: "Trace: log level must be aws.LogDebug",
level: "trace",
expected: aws.LogDebug,
},
{
name: "Debug: log level must be aws.LogOff",
level: "debug",
expected: aws.LogOff,
},
{
name: "Info: log level must be aws.LogOff",
level: "info",
expected: aws.LogOff,
},
{
name: "Error: log level must be aws.LogOff",
level: "error",
expected: aws.LogOff,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
globalSessionCache.clear()
sess, err := globalSessionCache.newSession(context.Background(), Options{
LogLevel: log.LevelFromString(tc.level),
})
if err != nil {
t.Fatal(err)
}

cfgLogLevel := *sess.Config.LogLevel
if diff := cmp.Diff(cfgLogLevel, tc.expected); diff != "" {
t.Errorf("%s: (-want +got):\n%v", tc.name, diff)
}
})
}
}

func valueAtPath(i interface{}, s string) interface{} {
v, err := awsutil.ValuesAtPath(i, s)
if err != nil || len(v) == 0 {
Expand Down
3 changes: 3 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"time"

"github.com/peak/s5cmd/log"
"github.com/peak/s5cmd/storage/url"
"github.com/peak/s5cmd/strutil"
)
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewRemoteClient(ctx context.Context, url *url.URL, opts Options) (*S3, erro
NoSignRequest: opts.NoSignRequest,
UseListObjectsV1: opts.UseListObjectsV1,
RequestPayer: opts.RequestPayer,
LogLevel: opts.LogLevel,
bucket: url.Bucket,
region: opts.region,
}
Expand All @@ -76,6 +78,7 @@ type Options struct {
DryRun bool
NoSignRequest bool
UseListObjectsV1 bool
LogLevel log.LogLevel
RequestPayer string
bucket string
region string
Expand Down