Skip to content

Commit 8eeca69

Browse files
authored
Control plane support log output as json (#7455)
* Control plane support log output as json Signed-off-by: zirain <[email protected]> * gen Signed-off-by: zirain <[email protected]> --------- Signed-off-by: zirain <[email protected]>
1 parent 2899416 commit 8eeca69

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

api/v1alpha1/envoygateway_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,22 @@ type EnvoyGatewayLogging struct {
199199
//
200200
// +kubebuilder:default={default: info}
201201
Level map[EnvoyGatewayLogComponent]LogLevel `json:"level,omitempty"`
202+
// Encoder defines the log encoder format.
203+
// If unspecified, defaults to "Text".
204+
//
205+
// +optional
206+
Encoder *EnvoyGatewayLogEncoder `json:"encoder,omitempty"`
202207
}
203208

209+
type EnvoyGatewayLogEncoder string
210+
211+
const (
212+
// EnvoyGatewayLogEncoderText defines the "Text" log encoder.
213+
EnvoyGatewayLogEncoderText EnvoyGatewayLogEncoder = "Text"
214+
// EnvoyGatewayLogEncoderJSON defines the "JSON" log encoder.
215+
EnvoyGatewayLogEncoderJSON EnvoyGatewayLogEncoder = "JSON"
216+
)
217+
204218
// EnvoyGatewayLogComponent defines a component that supports a configured logging level.
205219
// +kubebuilder:validation:Enum=default;provider;gateway-api;xds-translator;xds-server;xds;infrastructure;global-ratelimit
206220
type EnvoyGatewayLogComponent string

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/logging/log.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,21 @@ func (l Logger) Sugar() *zap.SugaredLogger {
112112

113113
func initZapLogger(w io.Writer, logging *egv1a1.EnvoyGatewayLogging, level egv1a1.LogLevel) *zap.Logger {
114114
parseLevel, _ := zapcore.ParseLevel(string(logging.DefaultEnvoyGatewayLoggingLevel(level)))
115-
core := zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), zapcore.AddSync(w), zap.NewAtomicLevelAt(parseLevel))
115+
cfg := zap.NewDevelopmentEncoderConfig()
116+
var encoder zapcore.Encoder
117+
logEncoder := egv1a1.EnvoyGatewayLogEncoderText
118+
if logging != nil && logging.Encoder != nil {
119+
logEncoder = *logging.Encoder
120+
}
121+
122+
switch logEncoder {
123+
case egv1a1.EnvoyGatewayLogEncoderJSON:
124+
encoder = zapcore.NewJSONEncoder(cfg)
125+
default:
126+
encoder = zapcore.NewConsoleEncoder(cfg)
127+
}
128+
129+
core := zapcore.NewCore(encoder, zapcore.AddSync(w), zap.NewAtomicLevelAt(parseLevel))
116130

117131
return zap.New(core, zap.AddCaller())
118132
}

site/content/en/latest/api/extension_types.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,21 @@ _Appears in:_
14231423
| `global-ratelimit` | LogComponentGlobalRateLimitRunner defines the "global-ratelimit" runner component.<br /> |
14241424

14251425

1426+
#### EnvoyGatewayLogEncoder
1427+
1428+
_Underlying type:_ _string_
1429+
1430+
1431+
1432+
_Appears in:_
1433+
- [EnvoyGatewayLogging](#envoygatewaylogging)
1434+
1435+
| Value | Description |
1436+
| ----- | ----------- |
1437+
| `Text` | EnvoyGatewayLogEncoderText defines the "Text" log encoder.<br /> |
1438+
| `JSON` | EnvoyGatewayLogEncoderJSON defines the "JSON" log encoder.<br /> |
1439+
1440+
14261441
#### EnvoyGatewayLogging
14271442

14281443

@@ -1436,6 +1451,7 @@ _Appears in:_
14361451
| Field | Type | Required | Default | Description |
14371452
| --- | --- | --- | --- | --- |
14381453
| `level` | _object (keys:[EnvoyGatewayLogComponent](#envoygatewaylogcomponent), values:[LogLevel](#loglevel))_ | true | \{ default:info \} | Level is the logging level. If unspecified, defaults to "info".<br />EnvoyGatewayLogComponent options: default/provider/gateway-api/xds-translator/xds-server/infrastructure/global-ratelimit.<br />LogLevel options: debug/info/error/warn. |
1454+
| `encoder` | _[EnvoyGatewayLogEncoder](#envoygatewaylogencoder)_ | false | | Encoder defines the log encoder format.<br />If unspecified, defaults to "Text". |
14391455

14401456

14411457
#### EnvoyGatewayMetricSink

0 commit comments

Comments
 (0)