Skip to content

Commit

Permalink
feat(KRY-636): Add support for multiple logging levels and clean up l…
Browse files Browse the repository at this point in the history
…ogging (#56)

Co-authored-by: Kote <km@projectn.co>
  • Loading branch information
kmushegi and Kote committed Jul 25, 2023
1 parent 7bf67c7 commit 28737ad
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ run the following command to learn more about the options:
go run main.go serve --help
```

### Logging

Sidekick supports a `--log-level` argument to control the logging level. By default, the logging level is set to `info`. However, you can set a more verbose log level, such as debug, to enable detailed debugging information. For all available logging options run `./sidekick --help`.

### Docker

Build the docker image:
Expand Down Expand Up @@ -110,7 +114,8 @@ You can find examples on how to setup your aws sdk clients to work with sidekick

You can find more information to integrated sidekick with 3 party tools/frameworks/services [here](./integrations/)

### Pre Built binaries
### Pre Built binaries

Sidekick binaries are hosted and released from Github. Please check our [releases page](./releases).
To download any release of our linux amd64 binary run:

Expand Down
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ func (a *Api) routeBase(w http.ResponseWriter, req *http.Request) {
func dumpRequest(logger *zap.Logger, boltReq *boltrouter.BoltRequest) {
boltDump, err := httputil.DumpRequest(boltReq.Bolt, true)
if err != nil {
logger.Error("bolt dump request", zap.Error(err))
logger.Error("dumping bolt request", zap.Error(err))
return
}

awsDump, err := httputil.DumpRequest(boltReq.Aws, true)
if err != nil {
logger.Error("aws dump request", zap.Error(err))
logger.Error("dumping aws request", zap.Error(err))
return
}

Expand Down
2 changes: 1 addition & 1 deletion api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (a *Api) sessionMiddleware(handler http.Handler) http.HandlerFunc {
if session.Logger().Level() == zap.DebugLevel {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
logger.Error("session dump request", zap.Error(err))
logger.Error("dumping session request", zap.Error(err))
return
}
logger.Debug("session request dump", zap.String("dump", string(dump)))
Expand Down
6 changes: 3 additions & 3 deletions boltrouter/bolt_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (br *BoltRouter) DoBoltRequest(logger *zap.Logger, boltReq *BoltRequest) (*
return nil, false, err
}

logger.Info("initial request target", zap.String("target", initialRequestTarget), zap.String("reason", reason))
logger.Debug("initial request target", zap.String("target", initialRequestTarget), zap.String("reason", reason))

if initialRequestTarget == "bolt" {
resp, err := br.boltHttpClient.Do(boltReq.Bolt)
Expand All @@ -177,7 +177,7 @@ func (br *BoltRouter) DoBoltRequest(logger *zap.Logger, boltReq *BoltRequest) (*
} else if !StatusCodeIs2xx(resp.StatusCode) && br.config.Failover {
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
logger.Warn("bolt request failed", zap.Int("statusCode", resp.StatusCode), zap.String("body", string(b)))
logger.Error("bolt request failed", zap.Int("statusCode", resp.StatusCode), zap.String("body", string(b)))
resp, err := http.DefaultClient.Do(boltReq.Aws)
return resp, true, err
}
Expand All @@ -188,7 +188,7 @@ func (br *BoltRouter) DoBoltRequest(logger *zap.Logger, boltReq *BoltRequest) (*
return resp, false, err
} else if !StatusCodeIs2xx(resp.StatusCode) && resp.StatusCode == 404 {
// if the request to AWS failed with 404: NoSuchKey, fall back to Bolt
logger.Warn("aws request failed, falling back to bolt", zap.Int("statusCode", resp.StatusCode))
logger.Error("aws request failed, falling back to bolt", zap.Int("statusCode", resp.StatusCode))
resp, err := br.boltHttpClient.Do(boltReq.Bolt)
return resp, true, err
}
Expand Down
9 changes: 3 additions & 6 deletions cmd/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"golang.org/x/term"
)

func NewLogger(verbose bool) *zap.Logger {
func NewLogger(logLevel zapcore.Level) *zap.Logger {
var logEncoder zapcore.Encoder
if !term.IsTerminal(int(os.Stdout.Fd())) {
encoderConfig := getEncoderConfig()
Expand All @@ -20,7 +20,7 @@ func NewLogger(verbose bool) *zap.Logger {
}

zapAtom := zap.NewAtomicLevel()
zapAtom.SetLevel(zapcore.InfoLevel)
zapAtom.SetLevel(logLevel)

ret := zap.New(
zapcore.NewCore(
Expand All @@ -31,11 +31,8 @@ func NewLogger(verbose bool) *zap.Logger {
zap.AddCaller(),
zap.AddCallerSkip(1),
)
zapAtom.SetLevel(zapcore.InfoLevel)

if verbose {
zapAtom.SetLevel(zapcore.DebugLevel)
}
zapAtom.SetLevel(logLevel)

return ret
}
Expand Down
27 changes: 23 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
)

Expand All @@ -28,7 +29,7 @@ func getVersion() string {

func init() {
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "make output more verbose")
rootCmd.PersistentFlags().StringP("log-level", "", "info", "log level. one of: debug, info, warn, error, fatal, panic")
rootCmd.PersistentFlags().StringP("config", "c", "", "read configuration from this file")
}

Expand All @@ -41,8 +42,26 @@ var rootCmd = &cobra.Command{
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
verbose, _ := cmd.Flags().GetBool("verbose")
rootLogger = NewLogger(verbose)
logLevel, _ := cmd.Flags().GetString("log-level")
var zapLogLevel zapcore.Level
switch logLevel {
case "debug":
zapLogLevel = zapcore.DebugLevel
case "info":
zapLogLevel = zapcore.InfoLevel
case "warn":
zapLogLevel = zapcore.WarnLevel
case "error":
zapLogLevel = zapcore.ErrorLevel
case "fatal":
zapLogLevel = zapcore.FatalLevel
case "panic":
zapLogLevel = zapcore.PanicLevel
default:
zapLogLevel = zapcore.InfoLevel
}
rootLogger = NewLogger(zapLogLevel)

OnShutdown(func() {
_ = rootLogger.Sync()
})
Expand Down Expand Up @@ -80,7 +99,7 @@ var rootCmd = &cobra.Command{
}()

fmt.Println(asciiArt)
rootLogger.Sugar().Infof("Version: %s", getVersion())
fmt.Printf("Version: %s\n", getVersion())

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestAws(t *testing.T) {

func SetupSidekick(t *testing.T, ctx context.Context) {
ctx, cancel := context.WithCancel(ctx)
logger := cmd.NewLogger(false)
logger := cmd.NewLogger(zap.DebugLevel)

go func() {
sigs := make(chan os.Signal, 1)
Expand Down

0 comments on commit 28737ad

Please sign in to comment.