-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Refactor logger #3924
Refactor logger #3924
Conversation
@balamurugana, thanks for your PR! By analyzing the history of the files in this pull request, we identified @harshavardhana, @krishnasrinivas and @abperiasamy to be potential reviewers. |
Codecov Report
@@ Coverage Diff @@
## master #3924 +/- ##
==========================================
+ Coverage 68.44% 68.61% +0.17%
==========================================
Files 173 173
Lines 23382 23407 +25
==========================================
+ Hits 16003 16060 +57
+ Misses 6317 6292 -25
+ Partials 1062 1055 -7
Continue to review full report at Codecov.
|
cmd/config-v15.go
Outdated
@@ -261,8 +259,11 @@ func validateConfig() error { | |||
} | |||
|
|||
// Validate logger field | |||
if err := srvCfg.Logger.Validate(); err != nil { | |||
return err | |||
if srvCfg.Logger != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using the srvCfg.Logger.Validate() style here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want below?
func (l *loggers) Validate() (err error) {
if l != nil {
fileLogger := srvCfg.Logger.GetFile()
if fileLogger.Enable && fileLogger.Filename == "" {
err := errors.New("Missing filename for enabled file logger")
}
}
return err
}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@balamurugana yes, 👍
cmd/file-logger.go
Outdated
} | ||
|
||
if _, err = logger.file.Write(msgBytes); err != nil { | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean 'return err' here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Fixing it.
cmd/log-level.go
Outdated
|
||
// IsHigher - check whether given log level is higher or not. | ||
func (level LogLevel) IsHigher(l logrus.Level) bool { | ||
return l > level.Level |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug > Fatal ?
Please add a comment if yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Done.
c073ec2
to
02ffe07
Compare
any reviews here @donatello ? |
Reviewing now. |
cmd/gateway-main.go
Outdated
@@ -141,12 +142,9 @@ func gatewayMain(ctx *cli.Context) { | |||
// default to "us-east-1" | |||
err := newGatewayConfig(accessKey, secretKey, "us-east-1") | |||
if err != nil { | |||
console.Fatalf("Unable to initialize gateway config. Error: %s", err) | |||
fatalIf(err, "Unable to initialize gateway config") | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If block not needed here. The fatalIf()
can replace the if block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
cmd/gateway-main.go
Outdated
@@ -155,7 +153,7 @@ func gatewayMain(ctx *cli.Context) { | |||
|
|||
newObject, err := newGatewayLayer(backendType, accessKey, secretKey) | |||
if err != nil { | |||
console.Fatalf("Unable to initialize gateway layer. Error: %s", err) | |||
fatalIf(err, "Unable to initialize gateway layer") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If block not needed here. The fatalIf() can replace the if block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
cmd/gateway-main.go
Outdated
@@ -193,7 +191,7 @@ func gatewayMain(ctx *cli.Context) { | |||
cert, key = getPublicCertFile(), getPrivateKeyFile() | |||
} | |||
if aerr := apiServer.ListenAndServe(cert, key); aerr != nil { | |||
console.Fatalf("Failed to start minio server. Error: %s\n", aerr) | |||
fatalIf(err, "Failed to start minio server") | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be aerr
instead of err
in fatalIf, and this block can be rewritten in fewer lines as:
aerr := apiServer.ListenAndServe(cert, key)
fatalIf(aerr, "Failed to start minio server")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
cmd/file-logger.go
Outdated
|
||
str += ":" + logger.Filename | ||
return str | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is better implemented with Sprintf, than by so many concatenations:
enableStr := "enabled"
if !logger.Enable {
enableStr = "disabled"
}
return fmt.Sprintf("file:%s:%s:%s", enableStr, logger.Level.String(), logger.Filename)
Arguably this is more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
// Fire - log entry handler to save logs. | ||
func (log *Logger) Fire(entry *logrus.Entry) (err error) { | ||
if err = log.consoleTarget.Fire(entry); err != nil { | ||
log.Printf("Unable to log to console target. %s\n", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we return here, since we found an error?
Otherwise, the next for block over-writes the error we found here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its OK to print the error to the console and continue. Returning error is not helpful, but required to work with logrus
interface compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
02ffe07
to
6ce0e10
Compare
cmd/console-logger.go
Outdated
} | ||
|
||
str += ":" + logger.Level.String() | ||
return str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part can also be improved using Sprintf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - one last place remaining where string concatenation can be improved with Sprintf.
5f0bf89
to
4aa12ce
Compare
This patch fixes below * Previously fatalIf() never writes log other than first logging target. * quiet flag is not honored to show progress messages other than startup messages. * Removes console package usage for progress messages.
4aa12ce
to
729bc0d
Compare
This patch fixes below
Description
Motivation and Context
How Has This Been Tested?
make test
Types of changes
Checklist: