Skip to content

Commit

Permalink
cmd/contour: Add support for json log formatter (#4486)
Browse files Browse the repository at this point in the history
Signed-off-by: Tero Saarni <tero.saarni@est.tech>
  • Loading branch information
tsaarni authored and skriss committed May 9, 2022
1 parent db72a4d commit ca1ad98
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelogs/unreleased/4486-tsaarni-small.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for Contour to produce logs in JSON format by specifying `--log-format=json` command line switch.
14 changes: 13 additions & 1 deletion cmd/contour/contour.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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():
Expand Down
2 changes: 2 additions & 0 deletions site/content/docs/main/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<log level>` | Enable Kubernetes client debug logging |
| `--log-format=<text|json>` | Log output format for Contour. Either text (default) or json. |

## Configuration File

Expand Down Expand Up @@ -446,6 +447,7 @@ connects to Contour:
| <nobr>--namespace</nobr> | 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. |
| <nobr>--xds-resource-version</nobr> | v3 | Currently, the only valid xDS API resource version is `v3`. |
| <nobr>--dns-lookup-family</nobr> | auto | Defines what DNS Resolution Policy to use for Envoy -> Contour cluster name lookup. Either v4, v6 or auto. |
| <nobr>--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
Expand Down

0 comments on commit ca1ad98

Please sign in to comment.