Skip to content

Commit

Permalink
APPS-1063 Expose a flag to configure structured logging format
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Dec 22, 2023
1 parent ea1a43e commit 752f49b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ Usage:
Use the following properties for service configuration [flags]
Flags:
-c, --config string configuration file path/URL
-h, --help help for Use
-l, --log string log level (default "DEBUG")
-v, --version version for Use
-c, --config string configuration file path/URL
-h, --help help for Use
-f, --log-format string log format (PLAIN, JSON) (default "PLAIN")
-l, --log-level string log level (default "DEBUG")
-v, --version version for Use
```

### Run
Expand Down
16 changes: 9 additions & 7 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
// run parses the CLI parameters and executes backup.
func run() int {
var (
configFile, logLevel string
configFile, logLevel, logFormat string
)

validateFlags := func(cmd *cobra.Command, args []string) error {
Expand All @@ -47,12 +47,13 @@ func run() int {
}

rootCmd.Flags().StringVarP(&configFile, "config", "c", "", "configuration file path/URL")
rootCmd.Flags().StringVarP(&logLevel, "log", "l", "DEBUG", "log level")
rootCmd.Flags().StringVarP(&logLevel, "log-level", "l", "DEBUG", "log level")
rootCmd.Flags().StringVarP(&logFormat, "log-format", "f", "PLAIN", "log format (PLAIN, JSON)")

rootCmd.RunE = func(cmd *cobra.Command, args []string) error {
slog.Info("Aerospike Backup Service", "commit", commit, "buildTime", buildTime)
// set default logger
slog.SetDefault(slog.New(util.LogHandler(logLevel)))
slog.SetDefault(slog.New(util.LogHandler(logLevel, logFormat)))
slog.Info("Aerospike Backup Service", "commit", commit, "buildTime", buildTime)
setConfigurationManager(configFile)
// read configuration file
config, err := readConfiguration()
Expand All @@ -65,15 +66,16 @@ func run() int {
schedulers := service.BuildBackupSchedulers(config)
service.ScheduleHandlers(ctx, schedulers)
// run HTTP server
return runHTTPServer(ctx, schedulers, config)
err = runHTTPServer(ctx, schedulers, config)
// shutdown shared resources
shared.Shutdown()
return err
}

err := rootCmd.Execute()
if err != nil {
slog.Error("Error in rootCmd.Execute", "err", err)
}
// shutdown shared resources
shared.Shutdown()

return util.ToExitVal(err)
}
Expand Down
23 changes: 17 additions & 6 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ import (

// LogHandler returns the application log handler with the
// configured level.
func LogHandler(level string) slog.Handler {
return slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel(level),
AddSource: true,
})
func LogHandler(level, format string) slog.Handler {
addSource := true
switch strings.ToUpper(format) {
case "PLAIN":
return slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel(level),
AddSource: addSource,
})
case "JSON":
return slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel(level),
AddSource: addSource,
})
default:
panic(fmt.Sprintf("unsupported log format: %s", format))
}
}

// logLevel returns a level for the given string name.
Expand All @@ -35,7 +46,7 @@ func logLevel(level string) slog.Level {
case "ERROR":
return slog.LevelError
default:
panic(fmt.Sprintf("invalid log level configuration: %s", level))
panic(fmt.Sprintf("invalid log level: %s", level))
}
}

Expand Down

0 comments on commit 752f49b

Please sign in to comment.