From a3aa3bcad03a84a05b092a3f72ac20fc2e118819 Mon Sep 17 00:00:00 2001 From: Keepers Date: Thu, 2 Feb 2023 13:34:53 -0700 Subject: [PATCH] Issue 2329 logfile 1 (#2344) ## Description Prevents showing the log file location when calling commands that defer to help output or env details. Certain cases aren't caught, such as when calling a command with no flags (ex: `corso backup create exchange`). In those cases we catch the lack of flags and manually display the usage within the command handler. ## Does this PR need a docs update or release note? - [x] :no_entry: No ## Type of change - [x] :broom: Tech Debt/Cleanup ## Issue(s) * #2329 ## Test Plan - [x] :muscle: Manual --- .gitignore | 3 +++ src/cli/cli.go | 8 ++++++++ src/pkg/logger/logger.go | 3 +++ 3 files changed, 14 insertions(+) diff --git a/.gitignore b/.gitignore index 46f5189b8..911d91a10 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,9 @@ .corso_test.toml .corso.toml +# Logging +.corso.log + # Build directories /bin /docker/bin diff --git a/src/cli/cli.go b/src/cli/cli.go index f202e4953..77a03b1a7 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -8,6 +8,7 @@ import ( "github.com/alcionai/clues" "github.com/spf13/cobra" + "golang.org/x/exp/slices" "github.com/alcionai/corso/src/cli/backup" "github.com/alcionai/corso/src/cli/config" @@ -51,6 +52,13 @@ func preRun(cc *cobra.Command, args []string) error { flagSl = append(flagSl, f) } + avoidTheseCommands := []string{ + "corso", "env", "help", "backup", "details", "list", "restore", "delete", "repo", "init", "connect", + } + if len(logger.LogFile) > 0 && !slices.Contains(avoidTheseCommands, cc.Use) { + print.Info(cc.Context(), "Logging to file: "+logger.LogFile) + } + log.Infow("cli command", "command", cc.CommandPath(), "flags", flagSl, "version", version.CurrentVersion()) return nil diff --git a/src/pkg/logger/logger.go b/src/pkg/logger/logger.go index 923af44c8..abbce9119 100644 --- a/src/pkg/logger/logger.go +++ b/src/pkg/logger/logger.go @@ -29,6 +29,8 @@ var ( DebugAPI bool readableOutput bool + + LogFile string ) type logLevel int @@ -118,6 +120,7 @@ func PreloadLoggingFlags() (string, string) { } if logfile != "stdout" && logfile != "stderr" { + LogFile = logfile logdir := filepath.Dir(logfile) print.Info(context.Background(), "Logging to file: "+logfile)