mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #13339 from ardaguclu/support-zap-console-encoding
This commit is contained in:
commit
691dcd51f6
@ -29,6 +29,9 @@ 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
|
||||
|
||||
|
38
client/pkg/logutil/log_format.go
Normal file
38
client/pkg/logutil/log_format.go
Normal 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)
|
||||
}
|
||||
}
|
45
client/pkg/logutil/log_format_test.go
Normal file
45
client/pkg/logutil/log_format_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -31,7 +31,7 @@ var DefaultZapLoggerConfig = zap.Config{
|
||||
Thereafter: 100,
|
||||
},
|
||||
|
||||
Encoding: "json",
|
||||
Encoding: DefaultLogFormat,
|
||||
|
||||
// copied from "zap.NewProductionEncoderConfig" with some updates
|
||||
EncoderConfig: zapcore.EncoderConfig{
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
@ -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,
|
||||
)
|
||||
|
@ -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)")
|
||||
|
||||
|
@ -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}'
|
||||
|
Loading…
x
Reference in New Issue
Block a user