Skip to content
This repository has been archived by the owner on Aug 18, 2022. It is now read-only.

Commit

Permalink
Discussion: Review logging / console output for --log-level support
Browse files Browse the repository at this point in the history
Fixes #64
  • Loading branch information
PaulDFoster committed Jun 22, 2021
1 parent 712084f commit f892b5e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aztfmod/rover/pkg/console"
"github.com/aztfmod/rover/pkg/custom"
"github.com/aztfmod/rover/pkg/landingzone"
"github.com/aztfmod/rover/pkg/logger"
"github.com/aztfmod/rover/pkg/symphony"
"github.com/aztfmod/rover/pkg/version"
"github.com/spf13/cobra"
Expand All @@ -38,6 +39,8 @@ It acts as a toolchain development environment to avoid impacting the local mach
to make sure that all contributors in the GitOps teams are using a consistent set of tools and version.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
console.DebugEnabled, _ = cmd.Flags().GetBool("debug")
logger.SetLogLevel(cmd.Flags().GetString("log-level"))
logger.LogEnvironment, _ = cmd.Flags().GetString("environment")
},
}

Expand All @@ -54,6 +57,8 @@ func GetVersion() string {

func init() {
rootCmd.PersistentFlags().Bool("debug", false, "log extra debug information, may contain secrets")
rootCmd.PersistentFlags().String("environment", "development", "set logging environment production|development")
rootCmd.PersistentFlags().String("log-level", "info", "set log level trace|debug|info|warn|error|fatal|panic")

command.ValidateDependencies()

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/briandowns/spinner v1.13.0
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/terraform-exec v0.13.3
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand Down
64 changes: 64 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package logger

import (
"os"
"path/filepath"
"strings"

"github.com/aztfmod/rover/pkg/command"
"github.com/aztfmod/rover/pkg/utils"
log "github.com/sirupsen/logrus"
)

var LogEnvironment = "production"
var logLevel = log.InfoLevel

func init() {
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
DisableColors: false,
FullTimestamp: false,
DisableTimestamp: true,
})

log.SetLevel(logLevel)

if LogEnvironment == "production" {
log.SetFormatter(&log.JSONFormatter{})
roverHomeDir, _ := utils.GetRoverDirectory()
roverHomeLogsDir := filepath.Join(roverHomeDir, "logs")
command.EnsureDirectory(roverHomeLogsDir)
roverLogFile := filepath.Join(roverHomeLogsDir, "rover.log")
file, err := os.OpenFile(roverLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
log.SetOutput(file)
} else {
log.Info("Failed to log to file, using default stderr")
}
} else {
log.SetReportCaller(true) // has performance impact but useful in debugging
}

}

func SetLogLevel(level string, err error) {
lowerLevel := strings.ToLower(level)
switch lowerLevel {
case "trace":
logLevel = log.TraceLevel
case "debug":
logLevel = log.DebugLevel
case "info":
logLevel = log.InfoLevel
case "warn":
logLevel = log.WarnLevel
case "error":
logLevel = log.ErrorLevel
case "fatal":
logLevel = log.FatalLevel
case "panic":
logLevel = log.PanicLevel
default:
logLevel = log.InfoLevel
}
}

0 comments on commit f892b5e

Please sign in to comment.