Merge pull request #13339 from ardaguclu/support-zap-console-encoding

This commit is contained in:
Sam Batschelet 2021-10-20 05:39:43 -04:00 committed by GitHub
commit 691dcd51f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 3 deletions

View File

@ -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 `wal` was moved to `storage/wal`
- Package `datadir` was moved to `storage/datadir` - 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 ### Metrics, Monitoring
See [List of metrics](https://etcd.io/docs/latest/metrics/) for all metrics per release. 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).

View File

@ -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)
}
}

View File

@ -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)
}
}
}
}

View File

@ -31,7 +31,7 @@ var DefaultZapLoggerConfig = zap.Config{
Thereafter: 100, Thereafter: 100,
}, },
Encoding: "json", Encoding: DefaultLogFormat,
// copied from "zap.NewProductionEncoderConfig" with some updates // copied from "zap.NewProductionEncoderConfig" with some updates
EncoderConfig: zapcore.EncoderConfig{ EncoderConfig: zapcore.EncoderConfig{

View File

@ -361,6 +361,8 @@ type Config struct {
Logger string `json:"logger"` Logger string `json:"logger"`
// LogLevel configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'. // LogLevel configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.
LogLevel string `json:"log-level"` LogLevel string `json:"log-level"`
// LogFormat set log encoding. Only supports json, console. Default is 'json'.
LogFormat string `json:"log-format"`
// LogOutputs is either: // LogOutputs is either:
// - "default" as os.Stderr, // - "default" as os.Stderr,
// - "stderr" as os.Stderr, // - "stderr" as os.Stderr,

View File

@ -106,6 +106,11 @@ func (cfg *Config) setupLogging() error {
copied.ErrorOutputPaths = errOutputPaths copied.ErrorOutputPaths = errOutputPaths
copied = logutil.MergeOutputPaths(copied) copied = logutil.MergeOutputPaths(copied)
copied.Level = zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(cfg.LogLevel)) 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 { if cfg.ZapLoggerBuilder == nil {
lg, err := copied.Build() lg, err := copied.Build()
if err != nil { if err != nil {
@ -130,10 +135,22 @@ func (cfg *Config) setupLogging() error {
lvl := zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(cfg.LogLevel)) 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 // WARN: do not change field names in encoder config
// journald logging writer assumes field names of "level" and "caller" // journald logging writer assumes field names of "level" and "caller"
cr := zapcore.NewCore( cr := zapcore.NewCore(
zapcore.NewJSONEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig), encoder,
syncer, syncer,
lvl, lvl,
) )

View File

@ -247,6 +247,7 @@ func newConfig() *config {
fs.StringVar(&cfg.ec.Logger, "logger", "zap", "Currently only supports 'zap' for structured logging.") 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.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.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.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)") 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)")

View File

@ -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. Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd, or list of comma separated output targets.
--log-level 'info' --log-level 'info'
Configures log level. Only supports debug, info, warn, error, panic, or fatal. 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 'false'
Enable log rotation of a single log-outputs file target. Enable log rotation of a single log-outputs file target.
--log-rotation-config-json '{"maxsize": 100, "maxage": 0, "maxbackups": 0, "localtime": false, "compress": false}' --log-rotation-config-json '{"maxsize": 100, "maxage": 0, "maxbackups": 0, "localtime": false, "compress": false}'