Skip to content

Commit

Permalink
feat: implement structured logging and remove glog
Browse files Browse the repository at this point in the history
This commit uses logrus to implement structured logging throughout the
repository.

Two new flags have been introduced for managing logging: `--log-level`
and `--log-format`.

Fix #712
Fix #506
  • Loading branch information
hbagdi committed Jul 8, 2020
1 parent edc272a commit 437a697
Show file tree
Hide file tree
Showing 25 changed files with 605 additions and 382 deletions.
19 changes: 19 additions & 0 deletions cli/ingress-controller/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func TestDefaults(t *testing.T) {
APIServerHost: "",
KubeConfigFilePath: "",

LogLevel: "info",
LogFormat: "text",

EnableProfiling: true,

ShowVersion: false,
Expand Down Expand Up @@ -179,6 +182,8 @@ func TestOverrideViaCLIFlags(t *testing.T) {
"--apiserver-host", "kube-apiserver.internal",
"--kubeconfig", "/path/to/kubeconfig",

"--log-format", "json",

"--profiling=false",
"--version",
"--anonymous-reports=false",
Expand Down Expand Up @@ -216,6 +221,9 @@ func TestOverrideViaCLIFlags(t *testing.T) {
APIServerHost: "kube-apiserver.internal",
KubeConfigFilePath: "/path/to/kubeconfig",

LogLevel: "info",
LogFormat: "json",

EnableProfiling: false,
ShowVersion: true,
AnonymousReports: false,
Expand All @@ -240,6 +248,8 @@ func TestOverrideViaEnvVars(t *testing.T) {
"CONTROLLER_KONG_ADMIN_CONCURRENCY": "100",
"CONTROLLER_KONG_ADMIN_TOKEN": "my-secret-token",

"CONTROLLER_LOG_LEVEL": "panic",

"CONTROLLER_KONG_CUSTOM_ENTITIES_SECRET": "foons/barsecretname",
}
for k, v := range envs {
Expand Down Expand Up @@ -280,6 +290,9 @@ func TestOverrideViaEnvVars(t *testing.T) {
APIServerHost: "",
KubeConfigFilePath: "",

LogLevel: "panic",
LogFormat: "text",

EnableProfiling: true,

ShowVersion: false,
Expand Down Expand Up @@ -336,6 +349,9 @@ func TestDeprecatedFlags(t *testing.T) {
APIServerHost: "",
KubeConfigFilePath: "",

LogLevel: "info",
LogFormat: "text",

EnableProfiling: true,

ShowVersion: false,
Expand Down Expand Up @@ -397,6 +413,9 @@ func TestDeprecatedFlagPrecedences(t *testing.T) {
APIServerHost: "",
KubeConfigFilePath: "",

LogLevel: "info",
LogFormat: "text",

EnableProfiling: true,

ShowVersion: false,
Expand Down
26 changes: 16 additions & 10 deletions cli/ingress-controller/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"
"time"

"github.com/golang/glog"
"github.com/spf13/pflag"
"github.com/spf13/viper"

Expand Down Expand Up @@ -75,6 +74,10 @@ type cliConfig struct {
SyncRateLimit float32
EnableReverseSync bool

// Logging
LogLevel string
LogFormat string

// k8s connection details
APIServerHost string
KubeConfigFilePath string
Expand Down Expand Up @@ -205,6 +208,14 @@ IP/hostname when the controller is being stopped.`)
`Define the sync frequency upper limit`)
flag.Bool("enable-reverse-sync", false, `Enable reverse checks from Kong to Kubernetes`)

// Logging
flags.String("log-level", "info",
`Level of logging for the controller. Allowed values are
trace, debug, info, warn, error, fatal and panic.`)
flags.String("log-format", "text",
`Format of logs of the controller. Allowed values are
text and json.`)

// k8s connection details
flags.String("apiserver-host", "",
`The address of the Kubernetes Apiserver to connect to in the format of
Expand All @@ -228,11 +239,6 @@ func parseFlags() (cliConfig, error) {

flagSet := flagSet()

// glog
// The error is being ignored here for unit testing,
// this always errors out in unit tests but succeeds in e2e runs.
_ = flag.Set("logtostderr", "true")

flagSet.AddGoFlagSet(flag.CommandLine)
if err := flagSet.Parse(os.Args); err != nil {
return cliConfig{}, err
Expand All @@ -251,10 +257,6 @@ func parseFlags() (cliConfig, error) {
return cliConfig{}, err
}

for key, value := range viper.AllSettings() {
glog.V(2).Infof("FLAG: --%s=%q", key, value)
}

var config cliConfig
// Admission controller server properties
config.AdmissionWebhookListen = viper.GetString("admission-webhook-listen")
Expand Down Expand Up @@ -338,6 +340,10 @@ func parseFlags() (cliConfig, error) {
config.SyncRateLimit = (float32)(viper.GetFloat64("sync-rate-limit"))
config.EnableReverseSync = viper.GetBool("enable-reverse-sync")

// Logging
config.LogLevel = viper.GetString("log-level")
config.LogFormat = viper.GetString("log-format")

// k8s connection details
config.APIServerHost = viper.GetString("apiserver-host")
config.KubeConfigFilePath = viper.GetString("kubeconfig")
Expand Down
Loading

0 comments on commit 437a697

Please sign in to comment.