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

Refactor logger #3924

Merged
merged 1 commit into from
Mar 23, 2017
Merged

Refactor logger #3924

merged 1 commit into from
Mar 23, 2017

Conversation

balamurugana
Copy link
Member

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.

Description

Motivation and Context

How Has This Been Tested?

  • make test
  • manual

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@mention-bot
Copy link

@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-io
Copy link

codecov-io commented Mar 18, 2017

Codecov Report

Merging #3924 into master will increase coverage by 0.17%.
The diff coverage is 64.17%.

@@            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
Impacted Files Coverage Δ
cmd/prepare-storage.go 62.06% <0%> (ø) ⬆️
cmd/gateway-main.go 0% <0%> (ø) ⬆️
cmd/namespace-lock.go 73.61% <100%> (ø) ⬆️
cmd/config-v16.go 85.22% <100%> (-0.15%) ⬇️
cmd/credential.go 92.45% <100%> (+31.88%) ⬆️
cmd/config-dir.go 100% <100%> (+6.81%) ⬆️
cmd/config-migrate.go 60.19% <100%> (+0.08%) ⬆️
cmd/update-main.go 53.59% <16.66%> (+1.98%) ⬆️
cmd/file-logger.go 16.66% <16.66%> (ø)
cmd/console-logger.go 57.14% <57.14%> (ø)
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 11e15f9...729bc0d. Read the comment docs.

@@ -261,8 +259,11 @@ func validateConfig() error {
}

// Validate logger field
if err := srvCfg.Logger.Validate(); err != nil {
return err
if srvCfg.Logger != nil {
Copy link
Member

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 ?

Copy link
Member Author

@balamurugana balamurugana Mar 21, 2017

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
}

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@balamurugana yes, 👍

}

if _, err = logger.file.Write(msgBytes); err != nil {
return nil
Copy link
Member

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 ?

Copy link
Member Author

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
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Done.

@balamurugana balamurugana force-pushed the clean-logger branch 2 times, most recently from c073ec2 to 02ffe07 Compare March 21, 2017 00:51
@harshavardhana
Copy link
Member

any reviews here @donatello ?

@donatello
Copy link
Member

any reviews here @donatello ?

Reviewing now.

@@ -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")
}
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -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")
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -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")
}
Copy link
Member

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")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


str += ":" + logger.Filename
return str
}
Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

str += ":" + logger.Level.String()
return str
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@donatello donatello left a 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.

@balamurugana balamurugana force-pushed the clean-logger branch 5 times, most recently from 5f0bf89 to 4aa12ce Compare March 23, 2017 16:13
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.
@harshavardhana harshavardhana merged commit d3cb79a into minio:master Mar 23, 2017
@balamurugana balamurugana deleted the clean-logger branch July 14, 2017 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants