Skip to content

Commit

Permalink
feat: ability to configure logging via env vars (#80)
Browse files Browse the repository at this point in the history
* remove logging from cli commands
* move logging configuration to connectors manage command

Signed-off-by: Mark deVilliers <markdevilliers@gmail.com>
  • Loading branch information
mdevilliers authored Apr 14, 2020
1 parent 29ef9cb commit 03ec72b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 101 deletions.
31 changes: 4 additions & 27 deletions cmd/connectctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,37 @@ package main

import (
"fmt"
"github.com/90poe/connectctl/internal/ctl/tasks"
"github.com/90poe/connectctl/pkg/client/connect"
"os"
"strings"

"github.com/90poe/connectctl/internal/ctl/tasks"
"github.com/90poe/connectctl/pkg/client/connect"

"github.com/90poe/connectctl/internal/ctl/connectors"
"github.com/90poe/connectctl/internal/ctl/plugins"
"github.com/90poe/connectctl/internal/ctl/version"
"github.com/90poe/connectctl/internal/logging"

"github.com/pkg/errors"

homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
cfgFile string
logLevel string
logFile string
logFormat string
cfgFile string
)

func main() {
rootCmd := &cobra.Command{
Use: "connectctl [command]",
Short: "A kafka connect CLI",
Long: "",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
err := logging.Configure(logLevel, logFile, logFormat)
if err != nil {
return errors.Wrap(err, "error configuring logging")
}
log.Info("connectctl, a Kafka Connect CLI")
return nil
},
RunE: func(c *cobra.Command, _ []string) error {
return c.Help()
},
}

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "", "", "Config file (default is $HOME/.connectctl.yaml)")
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "INFO", "Log level for the CLI (Optional)")
rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "", "", "A file to use for log output (Optional)")
rootCmd.PersistentFlags().StringVarP(&logFormat, "logformat", "", "TEXT", "Format for log output (Optional)")

_ = viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
_ = viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))
_ = viper.BindPFlag("logfile", rootCmd.PersistentFlags().Lookup("logfile"))
_ = viper.BindPFlag("logoutput", rootCmd.PersistentFlags().Lookup("logformat"))

viper.SetDefault("loglevel", "INFO")

rootCmd.AddCommand(connectors.Command())
rootCmd.AddCommand(plugins.Command())
Expand Down
10 changes: 2 additions & 8 deletions internal/ctl/connectors/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/90poe/connectctl/pkg/manager"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,14 +38,10 @@ func addConnectorCmd() *cobra.Command {
}

func doAddConnectors(cmd *cobra.Command, params *addConnectorsCmdParams) error {
clusterLogger := log.WithField("cluster", params.ClusterURL)
clusterLogger.Infof("adding connectors")

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
}
clusterLogger.WithField("config", config).Trace("add connectors configuration")

source, err := findSource(params.Files, params.Directory, params.EnvVar, cmd)

Expand All @@ -70,10 +65,9 @@ func doAddConnectors(cmd *cobra.Command, params *addConnectorsCmdParams) error {
if err != nil {
return errors.Wrap(err, "error creating connectors manager")
}
err = mngr.Add(connectors)
if err != nil {
if err = mngr.Add(connectors); err != nil {
return errors.Wrap(err, "error creating connectors")
}
clusterLogger.Infof("added connectors")
fmt.Println("added connectors")
return nil
}
18 changes: 5 additions & 13 deletions internal/ctl/connectors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/pkg/errors"

"github.com/jedib0t/go-pretty/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,14 +39,10 @@ func listConnectorsCmd() *cobra.Command {
}

func doListConnectors(_ *cobra.Command, params *listConnectorsCmdParams) error {
clusterLogger := log.WithField("cluster", params.ClusterURL)
clusterLogger.Debug("listing connectors")

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
}
clusterLogger.WithField("config", config).Trace("list connectors configuration")

userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version)

Expand All @@ -68,20 +63,19 @@ func doListConnectors(_ *cobra.Command, params *listConnectorsCmdParams) error {

switch params.Output {
case "json":
err := printConnectorsAsJSON(connectors, clusterLogger)
err := printConnectorsAsJSON(connectors)
if err != nil {
return errors.Wrap(err, "error printing connectors as JSON")
}
case "table":
printConnectorsAsTable(connectors, clusterLogger)
printConnectorsAsTable(connectors)
default:
clusterLogger.Errorf("invalid output format specified: %s", params.Output)
return fmt.Errorf("invalid output format specified: %s", params.Output)
}
return nil
}

func printConnectorsAsJSON(connectors []*manager.ConnectorWithState, logger *log.Entry) error {
logger.Debug("printing connectors as JSON")
func printConnectorsAsJSON(connectors []*manager.ConnectorWithState) error {
b, err := json.MarshalIndent(connectors, "", " ")
if err != nil {
return err
Expand All @@ -91,9 +85,7 @@ func printConnectorsAsJSON(connectors []*manager.ConnectorWithState, logger *log
return nil
}

func printConnectorsAsTable(connectors []*manager.ConnectorWithState, logger *log.Entry) {
logger.Debug("printing connectors as table")

func printConnectorsAsTable(connectors []*manager.ConnectorWithState) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Name", "State", "WorkerId", "Tasks", "Config"})
Expand Down
16 changes: 15 additions & 1 deletion internal/ctl/connectors/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/90poe/connectctl/internal/ctl"
"github.com/90poe/connectctl/internal/healthcheck"
"github.com/90poe/connectctl/internal/logging"
"github.com/90poe/connectctl/internal/version"
"github.com/90poe/connectctl/pkg/client/connect"
"github.com/90poe/connectctl/pkg/manager"
Expand Down Expand Up @@ -40,6 +41,9 @@ type manageDefaults struct {
GlobalConnectorRestartPeriod time.Duration `envconfig:"GLOBAL_CONNECTOR_RESTART_PERIOD"`
GlobalTaskRestartsMax int `envconfig:"GLOBAL_TASK_RESTARTS_MAX"`
GlobalTaskRestartPeriod time.Duration `envconfig:"GLOBAL_TASK_RESTART_PERIOD"`
LogLevel string `envconfig:"LOG_LEVEL"`
LogFile string `envconfig:"LOG_FILE"`
LogFormat string `envconfig:"LOG_FORMAT"`
}

func manageConnectorsCmd() *cobra.Command { // nolint: funlen
Expand All @@ -54,6 +58,8 @@ func manageConnectorsCmd() *cobra.Command { // nolint: funlen
GlobalConnectorRestartPeriod: 10 * time.Second,
GlobalTaskRestartsMax: 5,
GlobalTaskRestartPeriod: 10 * time.Second,
LogLevel: "INFO",
LogFormat: "TEXT",
}

manageCmd := &cobra.Command{
Expand Down Expand Up @@ -82,7 +88,7 @@ if you specify --once then it will sync once and then exit.`,
ctl.BindDurationVarP(manageCmd.Flags(), &params.SyncPeriod, params.SyncPeriod, "sync-period", "s", "how often to sync with the connect cluster")
ctl.BindDurationVar(manageCmd.Flags(), &params.InitialWaitPeriod, params.InitialWaitPeriod, "wait-period", "time period to wait before starting the first sync")

ctl.BindBoolVar(manageCmd.Flags(), &params.AllowPurge, false, "allow-purge", "if set connectctl will manage all connectors in a cluster. If connectors exist in the cluster that aren't specified in --files then the connectors will be deleted")
ctl.BindBoolVar(manageCmd.Flags(), &params.AllowPurge, false, "allow-purge", "if set connectctl will manage all connectors in a cluster. If connectors exist in the cluster that aren' t specified in --files then the connectors will be deleted")
ctl.BindBoolVar(manageCmd.Flags(), &params.AutoRestart, false, "auto-restart", "if set connectors and tasks that are failed with automatically be restarted")
ctl.BindBoolVar(manageCmd.Flags(), &params.RunOnce, false, "once", "if supplied sync will run once and command will exit")

Expand All @@ -99,10 +105,18 @@ if you specify --once then it will sync once and then exit.`,
ctl.BindIntVar(manageCmd.Flags(), &params.SyncErrorRetryMax, params.SyncErrorRetryMax, "sync-error-retry-max", "maximum times to ignore retryable errors whilst syncing")
ctl.BindDurationVar(manageCmd.Flags(), &params.SyncErrorRetryPeriod, params.SyncErrorRetryPeriod, "sync-error-retry-period", "period of time between retryable errors whilst syncing")

ctl.BindStringVarP(manageCmd.Flags(), &params.LogLevel, params.LogLevel, "loglevel", "l", "Log level for the CLI (Optional)")
ctl.BindStringVar(manageCmd.Flags(), &params.LogFile, params.LogFile, "logfile", "A file to use for log output (Optional)")
ctl.BindStringVar(manageCmd.Flags(), &params.LogFormat, params.LogFormat, "logformat", "Format for log output (Optional)")

return manageCmd
}

func doManageConnectors(cmd *cobra.Command, params *manageDefaults) error {
if err := logging.Configure(params.LogLevel, params.LogFile, params.LogFormat); err != nil {
return errors.Wrap(err, "error configuring logging")
}

logger := log.WithFields(log.Fields{
"cluster": params.ClusterURL,
"version": version.Version,
Expand Down
10 changes: 2 additions & 8 deletions internal/ctl/connectors/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/90poe/connectctl/pkg/manager"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -37,14 +36,10 @@ func pauseConnectorsCmd() *cobra.Command {
}

func doPauseConnectors(_ *cobra.Command, params *pauseConnectorsCmdParams) error {
clusterLogger := log.WithField("cluster", params.ClusterURL)
clusterLogger.Infof("pausing connectors: %s", params.Connectors)

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
}
clusterLogger.WithField("config", config).Trace("pause connectors configuration")

userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version)

Expand All @@ -58,11 +53,10 @@ func doPauseConnectors(_ *cobra.Command, params *pauseConnectorsCmdParams) error
return errors.Wrap(err, "error creating connectors manager")
}

err = mngr.Pause(params.Connectors)
if err != nil {
if err = mngr.Pause(params.Connectors); err != nil {
return errors.Wrap(err, "error pausing connectors")
}

clusterLogger.Info("connectors paused successfully")
fmt.Println("connectors paused successfully")
return nil
}
10 changes: 2 additions & 8 deletions internal/ctl/connectors/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/90poe/connectctl/pkg/manager"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -37,14 +36,10 @@ func removeConnectorCmd() *cobra.Command {
}

func doRemoveConnectors(_ *cobra.Command, params *removeConnectorsCmdParams) error {
clusterLogger := log.WithField("cluster", params.ClusterURL)
clusterLogger.Infof("removing connectors: %s", params.Connectors)

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
}
clusterLogger.WithField("config", config).Trace("remove connectors configuration")

userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version)

Expand All @@ -57,11 +52,10 @@ func doRemoveConnectors(_ *cobra.Command, params *removeConnectorsCmdParams) err
if err != nil {
return errors.Wrap(err, "error creating connectors manager")
}
err = mngr.Remove(params.Connectors)
if err != nil {
if err = mngr.Remove(params.Connectors); err != nil {
return errors.Wrap(err, "error removing connectors")
}

clusterLogger.Info("removed connectors")
fmt.Println("removed connectors")
return nil
}
10 changes: 2 additions & 8 deletions internal/ctl/connectors/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/90poe/connectctl/pkg/manager"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -51,14 +50,10 @@ func restartConnectorsCmd() *cobra.Command {
}

func doRestartConnectors(_ *cobra.Command, params *restartConnectorsCmdParams) error {
clusterLogger := log.WithField("cluster", params.ClusterURL)
clusterLogger.Infof("restarting connectors: %s", params.Connectors)

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
}
clusterLogger.WithField("config", config).Trace("restart connectors configuration")

userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version)

Expand All @@ -72,11 +67,10 @@ func doRestartConnectors(_ *cobra.Command, params *restartConnectorsCmdParams) e
return errors.Wrap(err, "error creating connectors manager")
}

err = mngr.Restart(params.Connectors, params.RestartTasks, params.ForceRestartTasks, params.TaskIDs)
if err != nil {
if err = mngr.Restart(params.Connectors, params.RestartTasks, params.ForceRestartTasks, params.TaskIDs); err != nil {
return errors.Wrap(err, "error restarting connectors")
}

clusterLogger.Info("connectors restarted successfully")
fmt.Printf("connectors restarted successfully")
return nil
}
10 changes: 2 additions & 8 deletions internal/ctl/connectors/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/90poe/connectctl/pkg/manager"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -37,14 +36,10 @@ func resumeConnectorsCmd() *cobra.Command {
}

func doResumeConnectors(_ *cobra.Command, params *resumeConnectorsCmdParams) error {
clusterLogger := log.WithField("cluster", params.ClusterURL)
clusterLogger.Infof("resuming onnectots: %s", params.Connectors)

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
}
clusterLogger.WithField("config", config).Trace("resume connectors configuration")

userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version)

Expand All @@ -58,11 +53,10 @@ func doResumeConnectors(_ *cobra.Command, params *resumeConnectorsCmdParams) err
return errors.Wrap(err, "error creating connectors manager")
}

err = mngr.Resume(params.Connectors)
if err != nil {
if err = mngr.Resume(params.Connectors); err != nil {
return errors.Wrap(err, "error resuming connectors")
}

clusterLogger.Info("connectors resumed successfully")
fmt.Println("connectors resumed successfully")
return nil
}
Loading

0 comments on commit 03ec72b

Please sign in to comment.