Skip to content

GT-345 Optional JSON logger format #1252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- (Feature) Optional ResignLeadership Action
- (Feature) Improve CRD Management and deprecate CRD Chart
- (Bugfix) Fix invalid Timeout calculation in case of ActionList
- (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
17 changes: 10 additions & 7 deletions cmd/cmd.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 Down Expand Up @@ -45,7 +45,6 @@ import (
"k8s.io/client-go/kubernetes"
typedCore "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"

"github.com/arangodb/kube-arangodb/pkg/api"
deploymentApi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
Expand Down Expand Up @@ -103,6 +102,7 @@ var (
hardLimit uint64
}

logFormat string
logLevels []string
serverOptions struct {
host string
Expand Down Expand Up @@ -183,6 +183,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.StringVar(&logFormat, "log.format", "pretty", "Set log format. Allowed values: 'pretty', 'JSON'. If empty, default format is used")
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 +273,12 @@ func executeMain(cmd *cobra.Command, args []string) {
logger.Err(err).Fatal("Unable to parse log level")
}

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

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

kl := logging.Global().RegisterAndGetLogger("klog", logging.Info)

klog.SetOutput(kl.InfoIO())
klog.Info("nice to meet you")
klog.Flush()
logger.Info("nice to meet you")

// Check operating mode
if !operatorOptions.enableDeployment && !operatorOptions.enableDeploymentReplication && !operatorOptions.enableStorage &&
Expand Down
2 changes: 2 additions & 0 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
- [Topology awareness](./topology_awareness.md)
- [Configuring timezone](./configuring_tz.md)
- [Operator API](./api.md)
- [Logging](./logging.md)

43 changes: 43 additions & 0 deletions docs/design/logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Logging configuration

## Operator logging

### Log level

To adjust logging level of the operator, you can use `operator.args` in chart template value
as described in [Additional configuration](./additional_configuration.md).

For example, to set log level to `INFO` and `DEBUG` for `requests` package, you can use the following value:
```yaml
operator:
args: ["--log.level=INFO", "--log.level=requests=DEBUG"]
```

### Log format

By default, operator logs in `pretty` format.

To switch logging format to the JSON, you can use `operator.args` in chart template value:
```yaml
operator:
args: ["--log.format=pretty"]
```

## ArangoDeployment logging

By default, ArangoDeployment logs in `pretty` format.

To switch logging format to the JSON we need to pass `--log.use-json-format` argument to the ArangoDB server in the deployment:
```yaml
apiVersion: database.arangodb.com/v1
kind: ArangoDeployment
metadata:
name: single
spec:
mode: Single
single:
args:
- --log.use-json-format
- --log.level=INFO
- --log.level=backup=TRACE
```
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/stretchr/testify v1.7.1
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
golang.org/x/text v0.3.6
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
Expand All @@ -56,7 +57,6 @@ require (
k8s.io/apiextensions-apiserver v0.18.3
k8s.io/apimachinery v0.22.15
k8s.io/client-go v12.0.0+incompatible
k8s.io/klog v1.0.0
sigs.k8s.io/yaml v1.2.0
)

Expand All @@ -67,7 +67,6 @@ require (
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/gin-contrib/pprof v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand Down Expand Up @@ -105,7 +104,6 @@ require (
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
Loading