From cb48d5109c4a2e393931418b3fa3b797d1defc4d Mon Sep 17 00:00:00 2001 From: Eugen Sumin Date: Fri, 19 Apr 2024 17:18:33 +0200 Subject: [PATCH] When building Kopia command line take LogLevel and FileLogLevel from env variables if not specified in parameters --- pkg/kopia/command/common.go | 37 +++++++++++++++++++++++++++++++++++-- pkg/kopia/command/const.go | 1 + 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/kopia/command/common.go b/pkg/kopia/command/common.go index 11a5f02cfc1..4c74e864fbd 100644 --- a/pkg/kopia/command/common.go +++ b/pkg/kopia/command/common.go @@ -15,16 +15,35 @@ package command import ( + "os" + "runtime" + "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 { @@ -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) } diff --git a/pkg/kopia/command/const.go b/pkg/kopia/command/const.go index 125e871975c..abe16ea9fb3 100644 --- a/pkg/kopia/command/const.go +++ b/pkg/kopia/command/const.go @@ -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"