Skip to content

Commit e8b0bd4

Browse files
authored
Introduce ENABLE_JSON_LOGGING env variable (#1158)
1 parent f273ea7 commit e8b0bd4

File tree

6 files changed

+22
-1
lines changed

6 files changed

+22
-1
lines changed

charts/postgres-operator/templates/deployment.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ spec:
3737
image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}"
3838
imagePullPolicy: {{ .Values.image.pullPolicy }}
3939
env:
40+
{{- if .Values.enableJsonLogging }}
41+
- name: ENABLE_JSON_LOGGING
42+
value: "true"
43+
{{- end }}
4044
{{- if eq .Values.configTarget "ConfigMap" }}
4145
- name: CONFIG_MAP_NAME
4246
value: {{ template "postgres-operator.fullname" . }}

charts/postgres-operator/values.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ podLabels: {}
1515

1616
configTarget: "ConfigMap"
1717

18+
# JSON logging format
19+
enableJsonLogging: false
20+
1821
# general configuration parameters
1922
configGeneral:
2023
# choose if deployment creates/updates CRDs with OpenAPIV3Validation

cmd/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"flag"
5-
"log"
5+
log "github.com/sirupsen/logrus"
66
"os"
77
"os/signal"
88
"sync"
@@ -36,6 +36,8 @@ func init() {
3636
flag.BoolVar(&config.NoTeamsAPI, "noteamsapi", false, "Disable all access to the teams API")
3737
flag.Parse()
3838

39+
config.EnableJsonLogging = os.Getenv("ENABLE_JSON_LOGGING") == "true"
40+
3941
configMapRawName := os.Getenv("CONFIG_MAP_NAME")
4042
if configMapRawName != "" {
4143

@@ -63,6 +65,9 @@ func init() {
6365
func main() {
6466
var err error
6567

68+
if config.EnableJsonLogging {
69+
log.SetFormatter(&log.JSONFormatter{})
70+
}
6671
log.SetOutput(os.Stdout)
6772
log.Printf("Spilo operator %s\n", version)
6873

docs/reference/command_line_and_environment.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ The following environment variables are accepted by the operator:
5656
* **CRD_READY_WAIT_INTERVAL**
5757
defines the interval between consecutive attempts waiting for the
5858
`postgresql` CRD to be created. The default is 5s.
59+
60+
* **ENABLE_JSON_LOGGING**
61+
Set to `true` for JSON formatted logging output.
62+
The default is false.

pkg/controller/controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ type Controller struct {
7171
// NewController creates a new controller
7272
func NewController(controllerConfig *spec.ControllerConfig, controllerId string) *Controller {
7373
logger := logrus.New()
74+
if controllerConfig.EnableJsonLogging {
75+
logger.SetFormatter(&logrus.JSONFormatter{})
76+
}
7477

7578
var myComponentName = "postgres-operator"
7679
if controllerId != "" {

pkg/spec/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ type ControllerConfig struct {
114114
CRDReadyWaitTimeout time.Duration
115115
ConfigMapName NamespacedName
116116
Namespace string
117+
118+
EnableJsonLogging bool
117119
}
118120

119121
// cached value for the GetOperatorNamespace

0 commit comments

Comments
 (0)