Skip to content

Commit 2064b6f

Browse files
committed
GT-345 Optional JSON logger format
1 parent 2bf6769 commit 2064b6f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- (Maintenance) Add & Enable YAML Linter
1515
- (Feature) Optional ResignLeadership Action
1616
- (Feature) Improve CRD Management and deprecate CRD Chart
17+
- (Feature) Optional JSON logger format
1718

1819
## [1.2.24](https://github.com/arangodb/kube-arangodb/tree/1.2.24) (2023-01-25)
1920
- (Bugfix) Fix deployment creation on ARM64

cmd/cmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -103,6 +103,7 @@ var (
103103
hardLimit uint64
104104
}
105105

106+
prettifyLog bool
106107
logLevels []string
107108
serverOptions struct {
108109
host string
@@ -183,6 +184,7 @@ func init() {
183184
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)")
184185
f.StringVar(&serverOptions.adminSecretName, "server.admin-secret-name", defaultAdminSecretName, "Name of secret containing username + password for login to the dashboard")
185186
f.BoolVar(&serverOptions.allowAnonymous, "server.allow-anonymous-access", false, "Allow anonymous access to the dashboard")
187+
f.BoolVar(&prettifyLog, "log.prettifyLog", true, "Prettify log output (if false, log output is in JSON format)")
186188
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(), ", ")))
187189
f.BoolVar(&apiOptions.enabled, "api.enabled", true, "Enable operator HTTP and gRPC API")
188190
f.IntVar(&apiOptions.httpPort, "api.http-port", defaultAPIHTTPPort, "HTTP API port to listen on")
@@ -272,6 +274,7 @@ func executeMain(cmd *cobra.Command, args []string) {
272274
logger.Err(err).Fatal("Unable to parse log level")
273275
}
274276

277+
logging.SetGlobal(prettifyLog)
275278
logging.Global().ApplyLogLevels(levels)
276279

277280
podNameParts := strings.Split(name, "-")

pkg/logging/global.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -21,18 +21,27 @@
2121
package logging
2222

2323
import (
24+
"io"
2425
"os"
2526
"time"
2627

2728
"github.com/rs/zerolog"
2829
)
2930

30-
var global = NewFactory(zerolog.New(zerolog.ConsoleWriter{
31-
Out: os.Stdout,
32-
TimeFormat: time.RFC3339Nano,
33-
NoColor: true,
34-
}).With().Timestamp().Logger())
31+
var global = SetGlobal(true)
3532

3633
func Global() Factory {
3734
return global
3835
}
36+
37+
func SetGlobal(isPretty bool) Factory {
38+
var w io.Writer = os.Stderr
39+
if isPretty {
40+
w = zerolog.ConsoleWriter{
41+
Out: os.Stdout,
42+
TimeFormat: time.RFC3339Nano,
43+
NoColor: true,
44+
}
45+
}
46+
return NewFactory(zerolog.New(w).With().Timestamp().Logger())
47+
}

0 commit comments

Comments
 (0)