Add json logger

This commit is contained in:
Daniel Tomcej
2020-05-26 22:26:03 -07:00
committed by GitHub
parent 821fd1a0cf
commit d04c2eff76
10 changed files with 83 additions and 44 deletions
+2 -10
View File
@@ -2,13 +2,11 @@ package cleanup
import (
"fmt"
"os"
"github.com/containous/maesh/cmd"
"github.com/containous/maesh/pkg/cleanup"
"github.com/containous/maesh/pkg/k8s"
"github.com/containous/traefik/v2/pkg/cli"
"github.com/sirupsen/logrus"
)
// NewCmd builds a new Cleanup command.
@@ -25,17 +23,11 @@ func NewCmd(cConfig *cmd.CleanupConfiguration, loaders []cli.ResourceLoader) *cl
}
func cleanupCommand(cConfig *cmd.CleanupConfiguration) error {
log := logrus.New()
log.SetOutput(os.Stdout)
logLevel, err := logrus.ParseLevel(cConfig.LogLevel)
log, err := cmd.BuildLogger(cConfig.LogFormat, cConfig.LogLevel, false)
if err != nil {
return err
return fmt.Errorf("could not build logger: %w", err)
}
log.SetLevel(logLevel)
log.Debugln("Starting maesh cleanup...")
log.Debugf("Using masterURL: %q", cConfig.MasterURL)
log.Debugf("Using kubeconfig: %q", cConfig.KubeConfig)
+6
View File
@@ -15,6 +15,7 @@ type MaeshConfiguration struct {
KubeConfig string `description:"Path to a kubeconfig. Only required if out-of-cluster." export:"true"`
MasterURL string `description:"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster." export:"true"`
LogLevel string `description:"The log level." export:"true"`
LogFormat string `description:"The log format." export:"true"`
Debug bool `description:"Debug mode, deprecated, use --loglevel=debug instead." export:"true"`
ACL bool `description:"Enable ACL mode." export:"true"`
SMI bool `description:"Enable SMI operation, deprecated, use --acl instead." export:"true"`
@@ -34,6 +35,7 @@ func NewMaeshConfiguration() *MaeshConfiguration {
ConfigFile: "",
KubeConfig: os.Getenv("KUBECONFIG"),
LogLevel: "error",
LogFormat: "common",
Debug: false,
ACL: false,
SMI: false,
@@ -52,6 +54,7 @@ type PrepareConfiguration struct {
KubeConfig string `description:"Path to a kubeconfig. Only required if out-of-cluster." export:"true"`
MasterURL string `description:"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster." export:"true"`
LogLevel string `description:"The log level." export:"true"`
LogFormat string `description:"The log format." export:"true"`
Debug bool `description:"Debug mode, deprecated, use --loglevel=debug instead." export:"true"`
Namespace string `description:"The namespace that maesh is installed in." export:"true"`
ClusterDomain string `description:"Your internal K8s cluster domain." export:"true"`
@@ -64,6 +67,7 @@ func NewPrepareConfiguration() *PrepareConfiguration {
return &PrepareConfiguration{
KubeConfig: os.Getenv("KUBECONFIG"),
LogLevel: "error",
LogFormat: "common",
Debug: false,
Namespace: "maesh",
ClusterDomain: "cluster.local",
@@ -105,6 +109,7 @@ type CleanupConfiguration struct {
MasterURL string `description:"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster." export:"true"`
Namespace string `description:"The namespace that maesh is installed in." export:"true"`
LogLevel string `description:"The log level." export:"true"`
LogFormat string `description:"The log format." export:"true"`
}
// NewCleanupConfiguration creates CleanupConfiguration.
@@ -113,5 +118,6 @@ func NewCleanupConfiguration() *CleanupConfiguration {
KubeConfig: os.Getenv("KUBECONFIG"),
Namespace: "maesh",
LogLevel: "error",
LogFormat: "common",
}
}
+55
View File
@@ -0,0 +1,55 @@
package cmd
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
)
// parseLogLevel parses a given log level and returns a standardized level.
func parseLogLevel(level string) (logrus.Level, error) {
return logrus.ParseLevel(level)
}
// parseLogFormat parses a log format and returns a formatter.
func parseLogFormat(format string) (logrus.Formatter, error) {
switch format {
case "json":
return &logrus.JSONFormatter{}, nil
case "common":
return &logrus.TextFormatter{DisableColors: false, FullTimestamp: true, DisableSorting: true}, nil
default:
return nil, fmt.Errorf("invalid logging format: %s", format)
}
}
// BuildLogger returns a formatted fieldlogger from the provided format, level, and debug configurations.
func BuildLogger(format, level string, debug bool) (logrus.FieldLogger, error) {
log := logrus.New()
log.SetOutput(os.Stdout)
logLevelStr := level
if debug {
logLevelStr = "debug"
log.Warnf("Debug flag is deprecated, please consider using --loglevel=DEBUG instead")
}
logLevel, err := parseLogLevel(logLevelStr)
if err != nil {
return log, err
}
log.SetLevel(logLevel)
logFormat, err := parseLogFormat(format)
if err != nil {
return log, err
}
log.SetFormatter(logFormat)
return log, nil
}
+2 -16
View File
@@ -15,7 +15,6 @@ import (
preparepkg "github.com/containous/maesh/pkg/prepare"
"github.com/containous/maesh/pkg/signals"
"github.com/containous/traefik/v2/pkg/cli"
"github.com/sirupsen/logrus"
)
func main() {
@@ -63,24 +62,11 @@ func main() {
}
func maeshCommand(iConfig *cmd.MaeshConfiguration) error {
log := logrus.New()
log.SetOutput(os.Stdout)
logLevelStr := iConfig.LogLevel
if iConfig.Debug {
logLevelStr = "debug"
log.Warnf("Debug flag is deprecated, please consider using --loglevel=DEBUG instead")
}
logLevel, err := logrus.ParseLevel(logLevelStr)
log, err := cmd.BuildLogger(iConfig.LogFormat, iConfig.LogLevel, iConfig.Debug)
if err != nil {
return err
return fmt.Errorf("could not build logger: %w", err)
}
log.SetLevel(logLevel)
log.Debugln("Starting maesh prepare...")
log.Debugf("Using masterURL: %q", iConfig.MasterURL)
log.Debugf("Using kubeconfig: %q", iConfig.KubeConfig)
+2 -17
View File
@@ -2,13 +2,11 @@ package prepare
import (
"fmt"
"os"
"github.com/containous/maesh/cmd"
"github.com/containous/maesh/pkg/k8s"
"github.com/containous/maesh/pkg/prepare"
"github.com/containous/traefik/v2/pkg/cli"
"github.com/sirupsen/logrus"
)
// NewCmd builds a new Prepare command.
@@ -25,24 +23,11 @@ func NewCmd(pConfig *cmd.PrepareConfiguration, loaders []cli.ResourceLoader) *cl
}
func prepareCommand(pConfig *cmd.PrepareConfiguration) error {
log := logrus.New()
log.SetOutput(os.Stdout)
logLevelStr := pConfig.LogLevel
if pConfig.Debug {
logLevelStr = "debug"
log.Warnf("Debug flag is deprecated, please consider using --loglevel=DEBUG instead")
}
logLevel, err := logrus.ParseLevel(logLevelStr)
log, err := cmd.BuildLogger(pConfig.LogFormat, pConfig.LogLevel, pConfig.Debug)
if err != nil {
return err
return fmt.Errorf("could not build logger: %w", err)
}
log.SetLevel(logLevel)
log.Debugln("Starting maesh prepare...")
log.Debugf("Using masterURL: %q", pConfig.MasterURL)
log.Debugf("Using kubeconfig: %q", pConfig.KubeConfig)
+1 -1
View File
@@ -7,7 +7,7 @@ The static configuration is configured when the service mesh is installed and is
- The Maesh image version can be manually defined if needed.
- Logging level for controller and proxies can be defined.
- Logging level and format for controller and proxies can be defined.
- The default mesh mode can be configured. If this is not set, the default mode will be HTTP.
This means that new mesh services that are not specified will default to operate in HTTP mode.
@@ -67,6 +67,9 @@ spec:
{{- if or .Values.controller.logLevel .Values.logLevel }}
- "--logLevel={{ or .Values.controller.logLevel .Values.logLevel }}"
{{- end }}
{{- if or .Values.controller.logFormat .Values.logFormat }}
- "--logFormat={{ or .Values.controller.logFormat .Values.logFormat }}"
{{- end }}
{{- if .Values.mesh.defaultMode }}
- "--defaultMode={{ .Values.mesh.defaultMode }}"
{{- end }}
@@ -111,6 +114,9 @@ spec:
{{- if or .Values.controller.logLevel .Values.logLevel }}
- "--logLevel={{ or .Values.controller.logLevel .Values.logLevel }}"
{{- end }}
{{- if or .Values.controller.logFormat .Values.logFormat }}
- "--logFormat={{ or .Values.controller.logFormat .Values.logFormat }}"
{{- end }}
{{- if .Values.acl }}
- "--acl"
{{- end }}
@@ -118,6 +118,9 @@ spec:
{{- if or .Values.controller.logLevel .Values.logLevel }}
- "--logLevel={{ or .Values.controller.logLevel .Values.logLevel }}"
{{- end }}
{{- if or .Values.controller.logFormat .Values.logFormat }}
- "--logFormat={{ or .Values.controller.logFormat .Values.logFormat }}"
{{- end }}
- "--namespace={{ .Release.Namespace }}"
securityContext:
capabilities:
@@ -174,6 +174,9 @@ spec:
{{- if or .Values.mesh.logLevel .Values.logLevel }}
- "--log.level={{ or .Values.mesh.logLevel .Values.logLevel }}"
{{- end }}
{{- if or .Values.mesh.logFormat .Values.logFormat }}
- "--log.format={{ or .Values.mesh.logFormat .Values.logFormat }}"
{{- end }}
{{- if .Values.metrics.prometheus.enabled }}
- "--metrics.prometheus"
{{- end }}
+3
View File
@@ -15,6 +15,7 @@ image:
# clusterDomain: cluster.local
# logLevel: error
# logFormat: common
limits:
http: 10
@@ -23,6 +24,7 @@ limits:
controller:
# logLevel: error
# logFormat: common
# ignoreNamespaces:
# - example
@@ -42,6 +44,7 @@ controller:
mesh:
# defaultMode: http
# logLevel: error
# logFormat: common
# pollInterval: 1s
# pollTimeout: 1s
# forwardingTimeouts: