Skip to content

Commit

Permalink
Added json format logging (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
applike-ss authored Dec 5, 2024
1 parent b1e8ac4 commit 91de5d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## main / unreleased

* [ENHANCEMENT] New parameter log.format allows to set logging format to logfmt (default) or json (new). #184

## v0.21.0

* [ENHANCEMENT] Log debug information about StatefulSets as they are created, updated and deleted. #182
Expand Down
19 changes: 15 additions & 4 deletions cmd/rollout-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import (
const defaultServerSelfSignedCertExpiration = model.Duration(365 * 24 * time.Hour)

type config struct {
logLevel string
logFormat string
logLevel string

serverPort int
kubeAPIURL string
Expand All @@ -65,6 +66,7 @@ type config struct {
}

func (cfg *config) register(fs *flag.FlagSet) {
fs.StringVar(&cfg.logFormat, "log.format", "logfmt", "The log format. Supported values: logfmt, json. Defaults to logfmt.")
fs.StringVar(&cfg.logLevel, "log.level", "debug", "The log level. Supported values: debug, info, warn, error.")
fs.IntVar(&cfg.serverPort, "server.port", 8001, "Port to use for exposing instrumentation and readiness probe endpoints.")
fs.StringVar(&cfg.kubeAPIURL, "kubernetes.api-url", "", "The Kubernetes server API URL. If not specified, it will be auto-detected when running within a Kubernetes cluster.")
Expand Down Expand Up @@ -113,7 +115,7 @@ func main() {
check(fs.Parse(os.Args[1:]))
check(cfg.validate())

logger, err := initLogger(cfg.logLevel)
logger, err := initLogger(cfg.logFormat, cfg.logLevel)
check(err)

reg := prometheus.NewRegistry()
Expand Down Expand Up @@ -280,8 +282,17 @@ func buildKubeConfig(apiURL, cfgFile string) (*rest.Config, error) {
return rest.InClusterConfig()
}

func initLogger(minLevel string) (log.Logger, error) {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
func initLogger(logFormat, minLevel string) (log.Logger, error) {
var logger log.Logger
switch logFormat {
case "logfmt":
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
case "json":
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
default:
return nil, fmt.Errorf("unknown log format: %s", logFormat)
}

var options []level.Option

switch minLevel {
Expand Down

0 comments on commit 91de5d2

Please sign in to comment.