From 3c27189ed61ad7d1535790ce2b5165d15e18118a Mon Sep 17 00:00:00 2001 From: nam Date: Thu, 13 Jun 2024 13:23:45 +0200 Subject: [PATCH] send to stdout as well, give option for compression and reduce default size --- cmd/readiness/main.go | 17 ++++++++++++++--- pkg/readiness/config/config.go | 27 ++++++++++++++++++--------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cmd/readiness/main.go b/cmd/readiness/main.go index c1d1cc4e3..9ed5b3e58 100644 --- a/cmd/readiness/main.go +++ b/cmd/readiness/main.go @@ -208,11 +208,22 @@ func parseHealthStatus(reader io.Reader) (health.Status, error) { } func initLogger(l *lumberjack.Logger) { - log := zap.New(zapcore.NewCore( + consoleCore := zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), zapcore.AddSync(os.Stdout), - zap.DebugLevel, - ), zap.Development()) + zap.DebugLevel) + + cores := []zapcore.Core{consoleCore} + if config.ReadBoolWitDefault(config.WithAgentFileLogging, "true") { + fileCore := zapcore.NewCore( + zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), + zapcore.AddSync(l), + zap.DebugLevel) + cores = append(cores, fileCore) + } + + core := zapcore.NewTee(cores...) + log := zap.New(core, zap.Development()) logger = log.Sugar() } diff --git a/pkg/readiness/config/config.go b/pkg/readiness/config/config.go index 4cba71dd7..aa0357c33 100644 --- a/pkg/readiness/config/config.go +++ b/pkg/readiness/config/config.go @@ -15,15 +15,17 @@ import ( const ( DefaultAgentHealthStatusFilePath = "/var/log/mongodb-mms-automation/agent-health-status.json" AgentHealthStatusFilePathEnv = "AGENT_STATUS_FILEPATH" + WithAgentFileLogging = "MDB_WITH_AGENT_FILE_LOGGING" - defaultLogPath = "/var/log/mongodb-mms-automation/readiness.log" - podNamespaceEnv = "POD_NAMESPACE" - automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint - logPathEnv = "LOG_FILE_PATH" - hostNameEnv = "HOSTNAME" - readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS" - readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE" - readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE" + defaultLogPath = "/var/log/mongodb-mms-automation/readiness.log" + podNamespaceEnv = "POD_NAMESPACE" + automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint + logPathEnv = "LOG_FILE_PATH" + hostNameEnv = "HOSTNAME" + readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS" + readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE" + readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE" + readinessProbeLoggerCompress = "READINESS_PROBE_LOGGER_COMPRESS" ) type Config struct { @@ -71,8 +73,9 @@ func GetLogger() *lumberjack.Logger { logger := &lumberjack.Logger{ Filename: readinessProbeLogFilePath(), MaxBackups: readIntOrDefault(readinessProbeLoggerBackups, 5), - MaxSize: readIntOrDefault(readinessProbeLoggerMaxSize, 10), + MaxSize: readIntOrDefault(readinessProbeLoggerMaxSize, 5), MaxAge: readInt(readinessProbeLoggerMaxAge), + Compress: ReadBoolWitDefault(readinessProbeLoggerCompress, "false"), } return logger } @@ -105,3 +108,9 @@ func readIntOrDefault(envVarName string, defaultValue int) int { } return intValue } + +// ReadBoolWitDefault returns the boolean value of an envvar of the given name. +func ReadBoolWitDefault(envVarName string, defaultValue string) bool { + envVar := GetEnvOrDefault(envVarName, defaultValue) + return strings.TrimSpace(strings.ToLower(envVar)) == "true" +}