From a7c67a089cfd9a422c8326ea7af3dce12a74d4f9 Mon Sep 17 00:00:00 2001 From: Shivaprasad Date: Wed, 6 Jul 2022 13:05:30 +0530 Subject: [PATCH] feat: add log init --- Makefile | 9 +++++++++ log/log.go | 2 +- moonshot.go | 13 ++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f1934b --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: tidy test + +tidy: + @echo "Tidy up..." + @go mod tidy -v + +test: + @echo "Running tests..." + @go test -cover ./... diff --git a/log/log.go b/log/log.go index 7f00c51..1bf68b2 100644 --- a/log/log.go +++ b/log/log.go @@ -10,7 +10,7 @@ var lg = logrus.New() // Setup configures the global logger instance with level and // formatter. -func Setup(format, level string) { +func Setup(level, format string) { lvl, err := logrus.ParseLevel(level) if err != nil { lvl = logrus.WarnLevel diff --git a/moonshot.go b/moonshot.go index 6aa926f..d841412 100644 --- a/moonshot.go +++ b/moonshot.go @@ -6,6 +6,7 @@ import ( "github.com/go-chi/chi" "github.com/spf13/cobra" + "github.com/spy16/moonshot/log" ) // App represents an instance of app command. Invoke App.Launch() @@ -27,7 +28,17 @@ func (app *App) Launch(ctx context.Context, cmds ...*cobra.Command) int { DisableDefaultCmd: true, }, } - root.PersistentFlags().StringP("config", "c", "", "Config file path override") + + flags := root.PersistentFlags() + + var logLevel, logFormat string + flags.StringP("config", "c", "", "Config file path override") + flags.StringVar(&logLevel, "log-level", "info", "Log level") + flags.StringVar(&logFormat, "log-format", "text", "Log format (json/text)") + + root.PersistentPreRun = func(cmd *cobra.Command, args []string) { + log.Setup(logLevel, logFormat) + } root.AddCommand(cmds...) root.AddCommand(