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

logging improvements #122

Merged
merged 4 commits into from
Oct 10, 2017
Merged

logging improvements #122

merged 4 commits into from
Oct 10, 2017

Conversation

skriss
Copy link
Contributor

@skriss skriss commented Oct 5, 2017

Fixes #116
Fixes #118
Fixes #120

@skriss skriss added this to the v0.5.0 milestone Oct 5, 2017
@skriss skriss changed the title [WIP] logging improvements logging improvements Oct 5, 2017
@skriss skriss requested review from ncdc and jrnt30 October 5, 2017 22:14
@skriss skriss force-pushed the logging-fixes branch 2 times, most recently from 9c7d0be to 9cd6001 Compare October 5, 2017 23:12
@@ -14,7 +14,8 @@ ark server [flags]
### Options

```
-h, --help help for server
-h, --help help for server
--log-level enum the logrus level at which to log (default info)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we spell out the options?

--log-level <level> the logrus level at which to log (..list of options)

I don't enum is self explanatory for new users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea, will do this.

logger.Hooks.Add(&logging.ErrorLocationHook{})
logLevel, err := logrus.ParseLevel(logLevelFlag.String())
if err != nil {
logLevel = logrus.InfoLevel
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we log (funny enough...) that the log level is invalid? Should it be an error?

Copy link
Contributor

Choose a reason for hiding this comment

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

I had the same thought. Probably a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do, although logically this case should never happen, since the enum flag will not allow an invalid value to be set.

@@ -14,7 +30,7 @@ const (

// ErrorLocationHook is a logrus hook that attaches error location information
// to log entries if an error is being logged and it has stack-trace information
// (i.e. if it originates from or is wrapped by github.com/pkg/errors)
// (i.e. if it originates from or is wrapped by github.com/pkg/errors).
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the out of context question, but wanted to validate an assumption I am making reading the code. If a user uses logger.Error("some error message") but not the logger.WithError(error) then the if errObj, exists = entry.Data[logrus.ErrorKey]; !exists will return true and short circuit, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, that case should be fine -- logger.Error(...) will set the msg and level fields, but not the error field so that check will return false. the error field is set either by calling .WithError or .WithField("error", err)

"github.com/sirupsen/logrus"
)

const logLocationField = "location"
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably should call this something a bit more descriptive (sourceFile, logSource, messageSource, etc)

@skriss skriss force-pushed the logging-fixes branch 2 times, most recently from 65ed673 to c422c19 Compare October 6, 2017 18:27
@skriss
Copy link
Contributor Author

skriss commented Oct 6, 2017

@jrnt30 @ncdc I addressed your feedback, PTAL at the last 2 commits.

// never happen assuming the enum flag is constructed correctly because the
// enum flag will not allow an invalid value to be set.
if err != nil {
logAtMinLevel(logger, "log-level flag has invalid value %s; setting log-level to %s", strings.ToUpper(logLevelFlag.String()), strings.ToUpper(logLevel.String()))
Copy link
Contributor

@jrnt30 jrnt30 Oct 10, 2017

Choose a reason for hiding this comment

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

This is clever but can we just assume that this is an error and log it as such but let execution continue? Especially given your context around it not really being possible in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, no need for logAtMinLevel. Also, as a follow-up to my comment above, I would just put this in an else block when parsing, and just do logrus.Errorf

logger.Hooks.Add(&logging.ErrorLocationHook{})
var (
logLevel = logrus.InfoLevel
parsed logrus.Level
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't declare this; it's only used by the ParseLevel call, so I'd recommend

if parsed, err := logrus.ParseLevel(logLevelFlag.String()); err == nil {
  logLevel = parsed
}

// never happen assuming the enum flag is constructed correctly because the
// enum flag will not allow an invalid value to be set.
if err != nil {
logAtMinLevel(logger, "log-level flag has invalid value %s; setting log-level to %s", strings.ToUpper(logLevelFlag.String()), strings.ToUpper(logLevel.String()))
Copy link
Contributor

Choose a reason for hiding this comment

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

+1, no need for logAtMinLevel. Also, as a follow-up to my comment above, I would just put this in an else block when parsing, and just do logrus.Errorf

}

return flag.NewEnum(logrus.InfoLevel.String(), logLevelsStrings...).
WithUsage(fmt.Sprintf("the logrus level at which to log (options %s)", strings.Join(logLevelsStrings, " | ")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need to mention logrus in the usage text

continue
}

entry.Data[logLocationField] = fmt.Sprintf("%s:%d", path.Base(frame.File), frame.Line)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want just the basename or the full path?

@skriss
Copy link
Contributor Author

skriss commented Oct 10, 2017

@ncdc @jrnt30 updated PTAL.

}

return flag.NewEnum(logrus.InfoLevel.String(), logLevelsStrings...).
WithUsage(fmt.Sprintf("the level at which to log (options %s)", strings.Join(logLevelsStrings, " | ")))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd do "the level at which to log. Valid values are %s" followed by strings.Join(logLevelsStrings, ", ")

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm tempted to suggest that instead of doing WithUsage, just make a helper that can retrieve a []string of sorted log levels, and use that inside of NewCommand, like this:

func NewCommand() *cobra.Command {
  sortedLogLevels := getSortedLogLevels()
  logLevelFlag := flag.NewEnum(logrus.InfoLevel.String(), sortedLogLevels...)
  // ...
  command.Flags().Var(logLevelFlag, "log-level", fmt.Sprintf("the level at which to log....."))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, that's reasonable.

@@ -95,7 +96,7 @@ func NewCommand() *cobra.Command {
},
}

command.Flags().Var(logLevelFlag, "log-level", logLevelFlag.Usage)
command.Flags().Var(logLevelFlag, "log-level", fmt.Sprintf("the level at which to log. Valid values are %s", strings.Join(sortedLogLevels, ", ")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a period to the valid values sentence please

@ncdc
Copy link
Contributor

ncdc commented Oct 10, 2017

Squash & let's roll

Signed-off-by: Steve Kriss <steve@heptio.com>
@skriss
Copy link
Contributor Author

skriss commented Oct 10, 2017

ready to go

Signed-off-by: Steve Kriss <steve@heptio.com>
Signed-off-by: Steve Kriss <steve@heptio.com>
Signed-off-by: Steve Kriss <steve@heptio.com>
@ncdc ncdc merged commit df3c514 into vmware-tanzu:master Oct 10, 2017
@skriss skriss deleted the logging-fixes branch October 10, 2017 20:25
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.

3 participants