Skip to content

Commit

Permalink
send to stdout as well, give option for compression and reduce defaul…
Browse files Browse the repository at this point in the history
…t size
  • Loading branch information
nammn committed Jun 13, 2024
1 parent d0c65eb commit 3c27189
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
17 changes: 14 additions & 3 deletions cmd/readiness/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
27 changes: 18 additions & 9 deletions pkg/readiness/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
}

0 comments on commit 3c27189

Please sign in to comment.