Skip to content

Commit

Permalink
GT-345 Optional JSON logger format
Browse files Browse the repository at this point in the history
  • Loading branch information
jwierzbo committed Feb 22, 2023
1 parent 2bf6769 commit d566044
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- (Maintenance) Add & Enable YAML Linter
- (Feature) Optional ResignLeadership Action
- (Feature) Improve CRD Management and deprecate CRD Chart
- (Feature) Optional JSON logger format

## [1.2.24](https://github.com/arangodb/kube-arangodb/tree/1.2.24) (2023-01-25)
- (Bugfix) Fix deployment creation on ARM64
Expand Down
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var (
hardLimit uint64
}

prettifyLog bool
logLevels []string
serverOptions struct {
host string
Expand Down Expand Up @@ -183,6 +184,7 @@ func init() {
f.StringVar(&serverOptions.tlsSecretName, "server.tls-secret-name", "", "Name of secret containing tls.crt & tls.key for HTTPS server (if empty, self-signed certificate is used)")
f.StringVar(&serverOptions.adminSecretName, "server.admin-secret-name", defaultAdminSecretName, "Name of secret containing username + password for login to the dashboard")
f.BoolVar(&serverOptions.allowAnonymous, "server.allow-anonymous-access", false, "Allow anonymous access to the dashboard")
f.BoolVar(&prettifyLog, "log.prettifyLog", true, "Prettify log output (if false, log output is in JSON format)")
f.StringArrayVar(&logLevels, "log.level", []string{defaultLogLevel}, fmt.Sprintf("Set log levels in format <level> or <logger>=<level>. Possible loggers: %s", strings.Join(logging.Global().Names(), ", ")))
f.BoolVar(&apiOptions.enabled, "api.enabled", true, "Enable operator HTTP and gRPC API")
f.IntVar(&apiOptions.httpPort, "api.http-port", defaultAPIHTTPPort, "HTTP API port to listen on")
Expand Down Expand Up @@ -272,6 +274,7 @@ func executeMain(cmd *cobra.Command, args []string) {
logger.Err(err).Fatal("Unable to parse log level")
}

logging.SetGlobal(prettifyLog)
logging.Global().ApplyLogLevels(levels)

podNameParts := strings.Split(name, "-")
Expand Down
21 changes: 15 additions & 6 deletions pkg/logging/global.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,18 +21,27 @@
package logging

import (
"io"
"os"
"time"

"github.com/rs/zerolog"
)

var global = NewFactory(zerolog.New(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339Nano,
NoColor: true,
}).With().Timestamp().Logger())
var global = SetGlobal(true)

func Global() Factory {
return global
}

func SetGlobal(isPretty bool) Factory {
var w io.Writer = os.Stderr
if isPretty {
w = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339Nano,
NoColor: true,
}
}
return NewFactory(zerolog.New(w).With().Timestamp().Logger())
}

0 comments on commit d566044

Please sign in to comment.