Skip to content

Commit

Permalink
When building Kopia command line take LogLevel and FileLogLevel from …
Browse files Browse the repository at this point in the history
…env variables if not specified in parameters
  • Loading branch information
e-sumin committed Apr 23, 2024
1 parent 08b8c79 commit cb48d51
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pkg/kopia/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,35 @@
package command

import (
"os"
"runtime"

Check failure on line 19 in pkg/kopia/command/common.go

View workflow job for this annotation

GitHub Actions / govulncheck

"runtime" imported and not used

"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/logsafe"
)

const (
// 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"
)

func LogLevel() string {
return os.Getenv(LogLevelVarName)
}

func FileLogLevel() string {
return os.Getenv(FileLogLevelVarName)
}

type CommandArgs struct {
RepoPassword string
ConfigFilePath string
LogDirectory string
LogLevel string
FileLogLevel string
}

func bashCommand(args logsafe.Cmd) []string {
Expand All @@ -40,12 +59,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
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)
}

if cmdArgs.ConfigFilePath != "" {
c = c.AppendLoggableKV(configFileFlag, cmdArgs.ConfigFilePath)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
legacyAPIFlag = "--legacy-api"
logDirectoryFlag = "--log-dir"
logLevelFlag = "--log-level"
fileLogLevelFlag = "--file-log-level"
LogLevelError = "error"
LogLevelInfo = "info"
noGrpcFlag = "--no-grpc"
Expand Down

0 comments on commit cb48d51

Please sign in to comment.