-
Notifications
You must be signed in to change notification settings - Fork 158
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
Take control over Kopia log level #2842
Conversation
cb48d51
to
ca853d0
Compare
…env variables if not specified in parameters
ca853d0
to
83ecc63
Compare
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.
Is it possible to add these flags to one of the command unit tests we have?
Good idea. I've added new unit test for common args builder. |
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
func FileLogLevel() string { | ||
return os.Getenv(FileLogLevelVarName) | ||
} | ||
|
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.
Hi Eugen,
First off, thanks for log flags! I've missed them recently (and hard-coded locally for tests) :).
What do you think about encapsulating the defaulting logic within envvar utility funcs?
For example, we could use a generic function like nonEmptyOrDefault
to handle defaults more elegantly.
Here’s an example of what this might look like:
const (
...
DefaultLogLevel = "info"
DefaultFileLogLevel = "debug"
)
func nonEmptyOrDefault[T comparable](t T, def T) T {
var empty T
if t != empty {
return t
}
return def
}
func getEnv(name, def string) string {
return nonEmptyOrDefault(os.Getenv(name), def)
}
func LogLevel() string {
return getEnv(LogLevelVarName, DefaultLogLevel)
}
func FileLogLevel() string {
return getEnv(FileLogLevelVarName, DefaultFileLogLevel)
}
and after we can refactor commonArgs
as:
OLD
...
logLevel := cmdArgs.LogLevel
if logLevel == "" {
logLevel = LogLevel()
}
if logLevel != "" {
c = c.AppendLoggableKV(logLevelFlag, logLevel)
} else {
c = c.AppendLoggableKV(logLevelFlag, LogLevelError)
}
fileLogLevel := cmdArgs.FileLogLevel
if fileLogLevel == "" {
fileLogLevel = FileLogLevel()
}
if fileLogLevel != "" {
c = c.AppendLoggableKV(fileLogLevelFlag, fileLogLevel)
}
...
NEW
...
logLevel := nonEmptyOrDefault(cmdArgs.LogLevel, LogLevel())
c = c.AppendLoggableKV(logLevelFlag, logLevel)
fileLogLevel := nonEmptyOrDefault(cmdArgs.FileLogLevel, FileLogLevel())
c = c.AppendLoggableKV(fileLogLevelFlag, fileLogLevel)
...
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.
accepted, done.
pkg/kopia/command/common.go
Outdated
// LogLevelVarName is the environment variable that controls Kopia log level. | ||
LogLevelVarName = "KOPIA_LOG_LEVEL" | ||
// FileLogLevelVarName is the environment variable that controls Kopia file log level. | ||
FileLogLevelVarName = "KOPIA_FILE_LOG_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.
If we are publishing these flags to users, IMO we shouldn't use KOPIA
term. We can use generic a term like DATA_STORE
. Users don't need to know about what tool we are using for data uploads. @pavannd1 @hairyhum @viveksinghggits wdyt?
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, I remember we discussed something like this.
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.
Thank you for pointing, renamed.
pkg/kopia/command/common.go
Outdated
LogLevelVarName = "KOPIA_LOG_LEVEL" | ||
// FileLogLevelVarName is the environment variable that controls Kopia file log level. | ||
FileLogLevelVarName = "KOPIA_FILE_LOG_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.
Just for my understand Eugen, can you please very briefly talk about what is the difference between Log Level and File Log 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.
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.
Thanks for @hairyhum for link.
Just to have an answer right here:
You can control how much data is written to console and log files by using flags:
--log-level - sets log level for console output (defaults to info)
--file-log-level - sets log level for file output (defaults to debug)
pkg/kopia/command/common.go
Outdated
) | ||
|
||
func LogLevel() string { | ||
return os.Getenv(LogLevelVarName) |
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.
how is this environment variable set? should we default to something if this env var is not set?
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.
In updated code, default value is provided.
pkg/kopia/command/common.go
Outdated
@@ -40,12 +58,26 @@ func stringSliceCommand(args logsafe.Cmd) []string { | |||
func commonArgs(cmdArgs *CommandArgs) logsafe.Cmd { | |||
c := logsafe.NewLoggable(kopiaCommand) | |||
|
|||
if cmdArgs.LogLevel != "" { | |||
c = c.AppendLoggableKV(logLevelFlag, cmdArgs.LogLevel) | |||
logLevel := cmdArgs.LogLevel |
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 think this requires change in the consumers of this function for example SnapshotCreate
etc? Are we planning to do that in other PR?
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 think it is not required.
Behavior before change:
Either level from var OR error
Behavior after change:
Either level from var OR level from env OR error
Taking level from env is needed only when debugging.
I went through the invokers and LogLevel is not set there. But if it will be set there to some value, I believe, decision of priority between env and their value should be done on their side.
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.
@e-sumin
+1: LG 👍 Thanks for doing this.
A couple of minor comments / questions.
|
||
var _ = check.Suite(&CommonUtilsSuite{}) | ||
|
||
func (s *CommonUtilsSuite) TestCommonArgs(c *check.C) { |
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.
These tests do not need to use check.v1
and could be built using the support in the plain Go testing
package.
Since this is (was) a new test, wouldn't it make sense to avoid check.v1
here?
}, { | ||
setup: func() func() { | ||
origLogLevel := os.Getenv(LogLevelVarName) | ||
os.Setenv(LogLevelVarName, "debug") |
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'd be good to use testing.T.Setenv
here to simplify the setup and make the test more robust, but that requires having access to testing.T
.
Change Overview
When building Kopia command line take LogLevel and FileLogLevel from env variables if it not specified in parameters
Pull request type
Please check the type of change your PR introduces:
Issues
Test Plan