Skip to content

Commit 4d8decd

Browse files
authored
GT-345 Optional JSON logger format (#1252)
1 parent 50a3aa4 commit 4d8decd

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- (Feature) Optional ResignLeadership Action
1616
- (Feature) Improve CRD Management and deprecate CRD Chart
1717
- (Bugfix) Fix invalid Timeout calculation in case of ActionList
18+
- (Feature) Optional JSON logger format
1819

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

cmd/cmd.go

Lines changed: 10 additions & 7 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.
@@ -45,7 +45,6 @@ import (
4545
"k8s.io/client-go/kubernetes"
4646
typedCore "k8s.io/client-go/kubernetes/typed/core/v1"
4747
"k8s.io/client-go/tools/record"
48-
"k8s.io/klog"
4948

5049
"github.com/arangodb/kube-arangodb/pkg/api"
5150
deploymentApi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
@@ -103,6 +102,7 @@ var (
103102
hardLimit uint64
104103
}
105104

105+
logFormat string
106106
logLevels []string
107107
serverOptions struct {
108108
host string
@@ -183,6 +183,7 @@ func init() {
183183
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)")
184184
f.StringVar(&serverOptions.adminSecretName, "server.admin-secret-name", defaultAdminSecretName, "Name of secret containing username + password for login to the dashboard")
185185
f.BoolVar(&serverOptions.allowAnonymous, "server.allow-anonymous-access", false, "Allow anonymous access to the dashboard")
186+
f.StringVar(&logFormat, "log.format", "pretty", "Set log format. Allowed values: 'pretty', 'JSON'. If empty, default format is used")
186187
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(), ", ")))
187188
f.BoolVar(&apiOptions.enabled, "api.enabled", true, "Enable operator HTTP and gRPC API")
188189
f.IntVar(&apiOptions.httpPort, "api.http-port", defaultAPIHTTPPort, "HTTP API port to listen on")
@@ -272,6 +273,12 @@ func executeMain(cmd *cobra.Command, args []string) {
272273
logger.Err(err).Fatal("Unable to parse log level")
273274
}
274275

276+
// Set root logger to stdout (JSON formatted) if not prettified
277+
if strings.ToUpper(logFormat) == "JSON" {
278+
logging.Global().SetRoot(zerolog.New(os.Stdout).With().Timestamp().Logger())
279+
} else if strings.ToLower(logFormat) != "pretty" && logFormat != "" {
280+
logger.Fatal("Unknown log format: %s", logFormat)
281+
}
275282
logging.Global().ApplyLogLevels(levels)
276283

277284
podNameParts := strings.Split(name, "-")
@@ -280,11 +287,7 @@ func executeMain(cmd *cobra.Command, args []string) {
280287
return in.Str("operator-id", operatorID)
281288
})
282289

283-
kl := logging.Global().RegisterAndGetLogger("klog", logging.Info)
284-
285-
klog.SetOutput(kl.InfoIO())
286-
klog.Info("nice to meet you")
287-
klog.Flush()
290+
logger.Info("nice to meet you")
288291

289292
// Check operating mode
290293
if !operatorOptions.enableDeployment && !operatorOptions.enableDeploymentReplication && !operatorOptions.enableStorage &&

docs/design/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
- [Topology awareness](./topology_awareness.md)
1515
- [Configuring timezone](./configuring_tz.md)
1616
- [Operator API](./api.md)
17+
- [Logging](./logging.md)
18+

docs/design/logging.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Logging configuration
2+
3+
## Operator logging
4+
5+
### Log level
6+
7+
To adjust logging level of the operator, you can use `operator.args` in chart template value
8+
as described in [Additional configuration](./additional_configuration.md).
9+
10+
For example, to set log level to `INFO` and `DEBUG` for `requests` package, you can use the following value:
11+
```yaml
12+
operator:
13+
args: ["--log.level=INFO", "--log.level=requests=DEBUG"]
14+
```
15+
16+
### Log format
17+
18+
By default, operator logs in `pretty` format.
19+
20+
To switch logging format to the JSON, you can use `operator.args` in chart template value:
21+
```yaml
22+
operator:
23+
args: ["--log.format=pretty"]
24+
```
25+
26+
## ArangoDeployment logging
27+
28+
By default, ArangoDeployment logs in `pretty` format.
29+
30+
To switch logging format to the JSON we need to pass `--log.use-json-format` argument to the ArangoDB server in the deployment:
31+
```yaml
32+
apiVersion: database.arangodb.com/v1
33+
kind: ArangoDeployment
34+
metadata:
35+
name: single
36+
spec:
37+
mode: Single
38+
single:
39+
args:
40+
- --log.use-json-format
41+
- --log.level=INFO
42+
- --log.level=backup=TRACE
43+
```

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/stretchr/testify v1.7.1
4949
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
5050
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
51+
golang.org/x/text v0.3.6
5152
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
5253
google.golang.org/grpc v1.47.0
5354
google.golang.org/protobuf v1.28.0
@@ -56,7 +57,6 @@ require (
5657
k8s.io/apiextensions-apiserver v0.18.3
5758
k8s.io/apimachinery v0.22.15
5859
k8s.io/client-go v12.0.0+incompatible
59-
k8s.io/klog v1.0.0
6060
sigs.k8s.io/yaml v1.2.0
6161
)
6262

@@ -67,7 +67,6 @@ require (
6767
github.com/cespare/xxhash/v2 v2.1.1 // indirect
6868
github.com/davecgh/go-spew v1.1.1 // indirect
6969
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
70-
github.com/gin-contrib/pprof v1.4.0 // indirect
7170
github.com/gin-contrib/sse v0.1.0 // indirect
7271
github.com/go-logr/logr v0.4.0 // indirect
7372
github.com/go-openapi/jsonpointer v0.19.5 // indirect
@@ -105,7 +104,6 @@ require (
105104
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
106105
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
107106
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
108-
golang.org/x/text v0.3.6 // indirect
109107
google.golang.org/appengine v1.6.7 // indirect
110108
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
111109
gopkg.in/inf.v0 v0.9.1 // indirect

0 commit comments

Comments
 (0)