From a336751d8334bd1b2c1349fa3fd1de6e4ae1ebf9 Mon Sep 17 00:00:00 2001 From: Tero Saarni Date: Tue, 19 Apr 2022 18:58:06 +0300 Subject: [PATCH] cmd/contour: Add support for json log formatter Signed-off-by: Tero Saarni --- changelogs/unreleased/4486-tsaarni-small.md | 1 + cmd/contour/contour.go | 14 +++++++++++++- site/content/docs/main/configuration.md | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/4486-tsaarni-small.md diff --git a/changelogs/unreleased/4486-tsaarni-small.md b/changelogs/unreleased/4486-tsaarni-small.md new file mode 100644 index 00000000000..d175d9eb31a --- /dev/null +++ b/changelogs/unreleased/4486-tsaarni-small.md @@ -0,0 +1 @@ +Add support for Contour to produce logs in JSON format by specifying `--log-format=json` command line switch. diff --git a/cmd/contour/contour.go b/cmd/contour/contour.go index c7123ee7977..9d2487d950b 100644 --- a/cmd/contour/contour.go +++ b/cmd/contour/contour.go @@ -32,6 +32,9 @@ func main() { app := kingpin.New("contour", "Contour Kubernetes ingress controller.") app.HelpFlag.Short('h') + // Log-format applies to log format of all sub-commands. + logFormat := app.Flag("log-format", "Log output format for Contour. Either text or json.").Default("text").Enum("text", "json") + envoyCmd := app.Command("envoy", "Sub-command for envoy actions.") sdm, shutdownManagerCtx := registerShutdownManager(envoyCmd, log) @@ -67,7 +70,16 @@ func main() { gatewayProvisioner, gatewayProvisionerConfig := registerGatewayProvisioner(app) args := os.Args[1:] - switch kingpin.MustParse(app.Parse(args)) { + cmd := kingpin.MustParse(app.Parse(args)) + + switch *logFormat { + case "text": + log.SetFormatter(&logrus.TextFormatter{}) + case "json": + log.SetFormatter(&logrus.JSONFormatter{}) + } + + switch cmd { case gatewayProvisioner.FullCommand(): runGatewayProvisioner(gatewayProvisionerConfig) case sdm.FullCommand(): diff --git a/site/content/docs/main/configuration.md b/site/content/docs/main/configuration.md index f9ce95353ea..df32be5cbc1 100644 --- a/site/content/docs/main/configuration.md +++ b/site/content/docs/main/configuration.md @@ -57,6 +57,7 @@ Many of these flags are mirrored in the [Contour Configuration File](#configurat | `--leader-election-resource-namespace` | The namespace of the resource (Lease) leader election will lease. | | `-d, --debug` | Enable debug logging | | `--kubernetes-debug=` | Enable Kubernetes client debug logging | +| `--log-format=` | Log output format for Contour. Either text (default) or json. | ## Configuration File @@ -446,6 +447,7 @@ connects to Contour: | --namespace | projectcontour | Namespace the Envoy container will run, also configured via ENV variable "CONTOUR_NAMESPACE". Namespace is used as part of the metric names on static resources defined in the bootstrap configuration file. | | --xds-resource-version | v3 | Currently, the only valid xDS API resource version is `v3`. | | --dns-lookup-family | auto | Defines what DNS Resolution Policy to use for Envoy -> Contour cluster name lookup. Either v4, v6 or auto. | +| --log-format | text | Log output format for Contour. Either text or json. | [1]: {{< param github_url>}}/tree/{{< param version >}}/examples/contour/01-contour-config.yaml