Skip to content

Commit

Permalink
Merge pull request #13339 from ardaguclu/support-zap-console-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
hexfusion authored Oct 20, 2021
2 parents 547ec8d + d7fa802 commit 691dcd5
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG-3.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ See [code changes](https://github.com/etcd-io/etcd/compare/v3.5.0...v3.6.0).
- Package `wal` was moved to `storage/wal`
- Package `datadir` was moved to `storage/datadir`

### etcd server

- Add [`etcd --log-format`](https://github.com/etcd-io/etcd/pull/13339) flag to support log format.

### Metrics, Monitoring

See [List of metrics](https://etcd.io/docs/latest/metrics/) for all metrics per release.

- Add [`etcd_disk_defrag_inflight`](https://github.com/etcd-io/etcd/pull/13371).
- Add [`etcd_disk_defrag_inflight`](https://github.com/etcd-io/etcd/pull/13371).
38 changes: 38 additions & 0 deletions client/pkg/logutil/log_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2019 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logutil

import "fmt"

const (
JsonLogFormat = "json"
ConsoleLogFormat = "console"
)

var DefaultLogFormat = JsonLogFormat

// ConvertToZapFormat converts and validated log format string.
func ConvertToZapFormat(format string) (string, error) {
switch format {
case ConsoleLogFormat:
return ConsoleLogFormat, nil
case JsonLogFormat:
return JsonLogFormat, nil
case "":
return DefaultLogFormat, nil
default:
return "", fmt.Errorf("unknown log format: %s, supported values json, console", format)
}
}
45 changes: 45 additions & 0 deletions client/pkg/logutil/log_format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logutil

import (
"testing"
)

func TestLogFormat(t *testing.T) {
tests := []struct {
given string
want string
errExpected bool
}{
{"json", JsonLogFormat, false},
{"console", ConsoleLogFormat, false},
{"", JsonLogFormat, false},
{"konsole", "", true},
}

for i, tt := range tests {
got, err := ConvertToZapFormat(tt.given)
if got != tt.want {
t.Errorf("#%d: ConvertToZapFormat failure: want=%v, got=%v", i, tt.want, got)
}

if err != nil {
if !tt.errExpected {
t.Errorf("#%d: ConvertToZapFormat unexpected error: %v", i, err)
}
}
}
}
2 changes: 1 addition & 1 deletion client/pkg/logutil/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var DefaultZapLoggerConfig = zap.Config{
Thereafter: 100,
},

Encoding: "json",
Encoding: DefaultLogFormat,

// copied from "zap.NewProductionEncoderConfig" with some updates
EncoderConfig: zapcore.EncoderConfig{
Expand Down
2 changes: 2 additions & 0 deletions server/embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ type Config struct {
Logger string `json:"logger"`
// LogLevel configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.
LogLevel string `json:"log-level"`
// LogFormat set log encoding. Only supports json, console. Default is 'json'.
LogFormat string `json:"log-format"`
// LogOutputs is either:
// - "default" as os.Stderr,
// - "stderr" as os.Stderr,
Expand Down
19 changes: 18 additions & 1 deletion server/embed/config_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (cfg *Config) setupLogging() error {
copied.ErrorOutputPaths = errOutputPaths
copied = logutil.MergeOutputPaths(copied)
copied.Level = zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(cfg.LogLevel))
encoding, err := logutil.ConvertToZapFormat(cfg.LogFormat)
if err != nil {
return err
}
copied.Encoding = encoding
if cfg.ZapLoggerBuilder == nil {
lg, err := copied.Build()
if err != nil {
Expand All @@ -130,10 +135,22 @@ func (cfg *Config) setupLogging() error {

lvl := zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(cfg.LogLevel))

var encoder zapcore.Encoder
encoding, err := logutil.ConvertToZapFormat(cfg.LogFormat)
if err != nil {
return err
}

if encoding == logutil.ConsoleLogFormat {
encoder = zapcore.NewConsoleEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig)
} else {
encoder = zapcore.NewJSONEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig)
}

// WARN: do not change field names in encoder config
// journald logging writer assumes field names of "level" and "caller"
cr := zapcore.NewCore(
zapcore.NewJSONEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig),
encoder,
syncer,
lvl,
)
Expand Down
1 change: 1 addition & 0 deletions server/etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func newConfig() *config {
fs.StringVar(&cfg.ec.Logger, "logger", "zap", "Currently only supports 'zap' for structured logging.")
fs.Var(flags.NewUniqueStringsValue(embed.DefaultLogOutput), "log-outputs", "Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd, or list of comma separated output targets.")
fs.StringVar(&cfg.ec.LogLevel, "log-level", logutil.DefaultLogLevel, "Configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.")
fs.StringVar(&cfg.ec.LogFormat, "log-format", logutil.DefaultLogFormat, "Configures log format. Only supports json, console. Default is 'json'.")
fs.BoolVar(&cfg.ec.EnableLogRotation, "enable-log-rotation", false, "Enable log rotation of a single log-outputs file target.")
fs.StringVar(&cfg.ec.LogRotationConfigJSON, "log-rotation-config-json", embed.DefaultLogRotationConfig, "Configures log rotation if enabled with a JSON logger config. Default: MaxSize=100(MB), MaxAge=0(days,no limit), MaxBackups=0(no limit), LocalTime=false(UTC), Compress=false(gzip)")

Expand Down
2 changes: 2 additions & 0 deletions server/etcdmain/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ Logging:
Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd, or list of comma separated output targets.
--log-level 'info'
Configures log level. Only supports debug, info, warn, error, panic, or fatal.
--log-format 'json'
Configures log format. Only supports json, console.
--enable-log-rotation 'false'
Enable log rotation of a single log-outputs file target.
--log-rotation-config-json '{"maxsize": 100, "maxage": 0, "maxbackups": 0, "localtime": false, "compress": false}'
Expand Down

0 comments on commit 691dcd5

Please sign in to comment.