-
Notifications
You must be signed in to change notification settings - Fork 1.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
logging improvements #122
logging improvements #122
Conversation
9c7d0be
to
9cd6001
Compare
docs/cli-reference/ark_server.md
Outdated
@@ -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) |
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.
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.
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.
good idea, will do this.
pkg/cmd/server/server.go
Outdated
logger.Hooks.Add(&logging.ErrorLocationHook{}) | ||
logLevel, err := logrus.ParseLevel(logLevelFlag.String()) | ||
if err != nil { | ||
logLevel = logrus.InfoLevel |
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.
Should we log (funny enough...) that the log level is invalid? Should it be an error?
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.
I had the same thought. Probably a good idea.
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.
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). |
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.
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?
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.
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" |
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.
Probably should call this something a bit more descriptive (sourceFile, logSource, messageSource, etc)
65ed673
to
c422c19
Compare
pkg/cmd/server/server.go
Outdated
// 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())) |
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 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.
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.
+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
pkg/cmd/server/server.go
Outdated
logger.Hooks.Add(&logging.ErrorLocationHook{}) | ||
var ( | ||
logLevel = logrus.InfoLevel | ||
parsed logrus.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.
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
}
pkg/cmd/server/server.go
Outdated
// 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())) |
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.
+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
pkg/cmd/server/server.go
Outdated
} | ||
|
||
return flag.NewEnum(logrus.InfoLevel.String(), logLevelsStrings...). | ||
WithUsage(fmt.Sprintf("the logrus level at which to log (options %s)", strings.Join(logLevelsStrings, " | "))) |
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.
Don't need to mention logrus in the usage text
continue | ||
} | ||
|
||
entry.Data[logLocationField] = fmt.Sprintf("%s:%d", path.Base(frame.File), frame.Line) |
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 just the basename or the full path?
pkg/cmd/server/server.go
Outdated
} | ||
|
||
return flag.NewEnum(logrus.InfoLevel.String(), logLevelsStrings...). | ||
WithUsage(fmt.Sprintf("the level at which to log (options %s)", strings.Join(logLevelsStrings, " | "))) |
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.
I'd do "the level at which to log. Valid values are %s" followed by strings.Join(logLevelsStrings, ", ")
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.
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....."))
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.
OK, that's reasonable.
pkg/cmd/server/server.go
Outdated
@@ -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, ", "))) |
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.
Add a period to the valid values sentence please
Squash & let's roll |
Signed-off-by: Steve Kriss <steve@heptio.com>
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>
Fixes #116
Fixes #118
Fixes #120