Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support single-line json as log format #1842

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions charts/connaisseur/templates/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data:
CACHE_EXPIRY_SECONDS: {{ dig "features" "cache" "expirySeconds" 30 . | quote }}
CACHE_ERRORS: {{ dig "features" "cache" "cacheErrors" true . | quote }}
LOG_LEVEL: {{ dig "logLevel" "info" . | quote }}
LOG_FORMAT: {{ dig "logFormat" "json-pretty" . | quote }}
{{- end }}
---
{{- if .Values.kubernetes.deployment.envs -}}
Expand Down
11 changes: 8 additions & 3 deletions cmd/connaisseur/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ func loadConfigs() (*config.Config, *alerting.Config) {
// Main function
func main() {
// set logging
logrus.SetFormatter(&logrus.JSONFormatter{
PrettyPrint: true,
})
switch utils.ConnaisseurLogFormat() {
case constants.LogFormatJson:
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
logrus.SetFormatter(&logrus.JSONFormatter{
PrettyPrint: true,
})
}
logrus.SetOutput(os.Stdout)
logrus.SetLevel(utils.ConnaisseurLogLevel())

Expand Down
8 changes: 8 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
DetectionMode = "DETECTION_MODE"
ResourceValidationMode = "RESOURCE_VALIDATION_MODE"
LogLevel = "LOG_LEVEL"
LogFormat = "LOG_FORMAT"
PodName = "POD_NAME"
DefaultAuthFile = "secret.yaml"
DockerAuthFile = ".dockerconfigjson"
Expand Down Expand Up @@ -82,3 +83,10 @@ const (
NotificationResultTimeout = "timeout"
NotificationResultInvalid = "invalid"
)

type ConnaisseurLogFormat uint32

const (
LogFormatJson ConnaisseurLogFormat = iota
LogFormatJsonPretty
)
16 changes: 16 additions & 0 deletions internal/utils/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ func ConnaisseurLogLevel() logrus.Level {
return LogLevel(os.Getenv(constants.LogLevel))
}

func LogFormatter(logFormat string) constants.ConnaisseurLogFormat {
switch strings.ToLower(logFormat) {
case "json":
return constants.LogFormatJson
case "json-pretty":
return constants.LogFormatJsonPretty
default:
// default for backwards compatibility
return constants.LogFormatJsonPretty
}
}

func ConnaisseurLogFormat() constants.ConnaisseurLogFormat {
return LogFormatter(os.Getenv(constants.LogFormat))
}

func InitiateThirdPartyLibraryLogging() {
currentLevel := ConnaisseurLogLevel()
// Cosign uses debug logs.Debug to redirect registry request log
Expand Down
21 changes: 21 additions & 0 deletions internal/utils/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ func TestConnaisseurLogLevel(t *testing.T) {
}
}

func TestConnaisseurLogFormat(t *testing.T) {
var testCases = []struct {
logFormat string
want constants.ConnaisseurLogFormat
}{
{
logFormat: "json",
want: constants.LogFormatJson,
},
{
logFormat: "json-pretty",
want: constants.LogFormatJsonPretty,
},
}
for _, tc := range testCases {
(*testing.T).Setenv(t, constants.LogFormat, tc.logFormat)
logFormat := ConnaisseurLogFormat()
assert.Equal(t, tc.want, logFormat)
}
}

func TestCompare(t *testing.T) {
var testCases = []struct {
level1 logrus.Level
Expand Down