Skip to content

Commit

Permalink
feat: add log init
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Jul 6, 2022
1 parent 8a19c07 commit a7c67a0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
all: tidy test

tidy:
@echo "Tidy up..."
@go mod tidy -v

test:
@echo "Running tests..."
@go test -cover ./...
2 changes: 1 addition & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion moonshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand Down

0 comments on commit a7c67a0

Please sign in to comment.