From cdd95b148515585c662ae97a67889ce8e6cc24b2 Mon Sep 17 00:00:00 2001 From: Tim Harrold Date: Mon, 4 Dec 2023 21:25:37 +0000 Subject: [PATCH] Update logger Config setter access pattern --- agent/app/run.go | 6 +- .../amazon-ecs-agent/ecs-agent/logger/log.go | 188 ++++++++++++------ ecs-agent/logger/log.go | 188 ++++++++++++------ ecs-agent/logger/log_test.go | 15 +- ecs-agent/logger/log_unix_test.go | 95 +++++++++ ecs-agent/logger/log_windows_test.go | 10 + 6 files changed, 366 insertions(+), 136 deletions(-) diff --git a/agent/app/run.go b/agent/app/run.go index e6fdb37ca32..0ee12573c40 100644 --- a/agent/app/run.go +++ b/agent/app/run.go @@ -60,9 +60,11 @@ func Run(arguments []string) int { } if *parsedArgs.LogLevel != "" { - logger.SetLevel(*parsedArgs.LogLevel, *parsedArgs.LogLevel) + logger.SetDriverLogLevel(*parsedArgs.LogLevel) + logger.SetInstanceLogLevel(*parsedArgs.LogLevel) } else { - logger.SetLevel(*parsedArgs.DriverLogLevel, *parsedArgs.InstanceLogLevel) + logger.SetDriverLogLevel(*parsedArgs.DriverLogLevel) + logger.SetInstanceLogLevel(*parsedArgs.InstanceLogLevel) } // Create an Agent object diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/log.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/log.go index fc7025f823c..4eb2a248543 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/log.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/log.go @@ -44,18 +44,34 @@ const ( DEFAULT_TIMESTAMP_FORMAT = time.RFC3339 DEFAULT_MAX_FILE_SIZE float64 = 10 DEFAULT_MAX_ROLL_COUNT int = 24 + DEFAULT_LOGTO_STDOUT = true ) +// Because timestamp format will be called in the custom formatter +// for each log message processed, it should not be handled +// with an explicitly write protected configuration. +var timestampFormat = DEFAULT_TIMESTAMP_FORMAT + +// logLevels is the mapping from ECS_LOGLEVEL to Seelog provided levels. +var logLevels = map[string]string{ + "debug": "debug", + "info": "info", + "warn": "warn", + "error": "error", + "crit": "critical", + "none": "off", +} + type logConfig struct { - RolloverType string - MaxRollCount int - MaxFileSizeMB float64 - logfile string - driverLevel string - instanceLevel string - outputFormat string - timestampFormat string - lock sync.Mutex + RolloverType string + MaxRollCount int + MaxFileSizeMB float64 + logfile string + driverLevel string + instanceLevel string + outputFormat string + logToStdout bool + lock sync.Mutex } var Config *logConfig @@ -89,7 +105,7 @@ func logfmtFormatter(params string) seelog.FormatterFunc { buf.WriteString(level.String()) buf.WriteByte(' ') buf.WriteString("time=") - buf.WriteString(context.CallTime().UTC().Format(Config.timestampFormat)) + buf.WriteString(context.CallTime().UTC().Format(timestampFormat)) buf.WriteByte(' ') // temporary measure to make this change backwards compatible as we update to structured logs if strings.HasPrefix(message, structuredTxtFormatPrefix) { @@ -114,7 +130,7 @@ func jsonFormatter(params string) seelog.FormatterFunc { buf.WriteString(`{"level":"`) buf.WriteString(level.String()) buf.WriteString(`","time":"`) - buf.WriteString(context.CallTime().UTC().Format(Config.timestampFormat)) + buf.WriteString(context.CallTime().UTC().Format(timestampFormat)) buf.WriteString(`",`) // temporary measure to make this change backwards compatible as we update to structured logs if strings.HasPrefix(message, structuredJsonFormatPrefix) { @@ -144,14 +160,21 @@ func reloadConfig() { } func seelogConfig() string { + platformLogConfig := platformLogConfig() c := ` - + ` + if platformLogConfig != "" || Config.logToStdout { + c += ` - ` - c += platformLogConfig() - c += ` + ` + if Config.logToStdout { + c += ` ` + } + c += platformLogConfig + c += ` ` + } if Config.logfile != "" { c += ` ` @@ -159,10 +182,13 @@ func seelogConfig() string { c += ` ` - } else { + } else if Config.RolloverType == "date" { c += ` ` + } else { + c += ` + ` } c += ` ` @@ -191,33 +217,6 @@ func getLevelList(fileLevel string) string { return levelLists[fileLevel] } -// SetLevel sets the log levels for logging -func SetLevel(driverLogLevel, instanceLogLevel string) { - levels := map[string]string{ - "debug": "debug", - "info": "info", - "warn": "warn", - "error": "error", - "crit": "critical", - "none": "off", - } - - parsedDriverLevel, driverOk := levels[strings.ToLower(driverLogLevel)] - parsedInstanceLevel, instanceOk := levels[strings.ToLower(instanceLogLevel)] - - if instanceOk || driverOk { - Config.lock.Lock() - defer Config.lock.Unlock() - if instanceOk { - Config.instanceLevel = parsedInstanceLevel - } - if driverOk { - Config.driverLevel = parsedDriverLevel - } - reloadConfig() - } -} - // GetLevel gets the log level func GetLevel() string { Config.lock.Lock() @@ -236,12 +235,41 @@ func setInstanceLevelDefault() string { return DEFAULT_LOGLEVEL } +// SetInstanceLogLevel explicitly sets the log level for instance logs. +func SetInstanceLogLevel(instanceLogLevel string) { + parsedLevel, ok := logLevels[strings.ToLower(instanceLogLevel)] + if ok { + Config.lock.Lock() + defer Config.lock.Unlock() + Config.instanceLevel = parsedLevel + reloadConfig() + } else { + seelog.Error("Instance log level mapping not found") + } +} + +// SetDriverLogLevel explicitly sets the log level for a custom driver. +func SetDriverLogLevel(driverLogLevel string) { + parsedLevel, ok := logLevels[strings.ToLower(driverLogLevel)] + if ok { + Config.lock.Lock() + defer Config.lock.Unlock() + Config.driverLevel = parsedLevel + reloadConfig() + } else { + seelog.Error("Driver log level mapping not found") + } +} + // SetConfigLogFile sets the default output file of the logger. func SetConfigLogFile(logFile string) { - Config.lock.Lock() - defer Config.lock.Unlock() + if logFile != "" { + Config.lock.Lock() + defer Config.lock.Unlock() - Config.logfile = logFile + Config.logfile = logFile + reloadConfig() + } } // SetConfigLogFormat sets the output format of the logger. @@ -250,20 +278,33 @@ func SetConfigLogFormat(logFormat string) { Config.lock.Lock() defer Config.lock.Unlock() - os.Setenv(LOG_OUTPUT_FORMAT_ENV_VAR, logFormat) Config.outputFormat = logFormat + reloadConfig() } // SetConfigMaxFileSizeMB sets the max file size of a log file // in Megabytes before the logger rotates to a new file. func SetConfigMaxFileSizeMB(maxSizeInMB float64) { - Config.lock.Lock() - defer Config.lock.Unlock() + if maxSizeInMB > 0 { + Config.lock.Lock() + defer Config.lock.Unlock() - // Parse unit as string to set as environment variable. - strsize := strconv.FormatFloat(maxSizeInMB, 'f', -1, 64) - os.Setenv(LOG_MAX_FILE_SIZE_ENV_VAR, strsize) - Config.MaxFileSizeMB = maxSizeInMB + Config.MaxFileSizeMB = maxSizeInMB + reloadConfig() + } +} + +// SetRolloverType sets the logging rollover constraint. +// This should be either size or date. Logger will roll +// to a new log file based on this constraint. +func SetRolloverType(rolloverType string) { + if rolloverType == "date" || rolloverType == "size" { + Config.lock.Lock() + defer Config.lock.Unlock() + + Config.RolloverType = rolloverType + reloadConfig() + } } // SetTimestampFormat sets the time formatting @@ -271,27 +312,38 @@ func SetConfigMaxFileSizeMB(maxSizeInMB float64) { // a valid time format such as time.RFC3339 // or "2006-01-02T15:04:05.000". func SetTimestampFormat(format string) { + if format != "" { + timestampFormat = format + } +} + +// SetlogToStdout decides whether the logger +// should write to stdout using the tag +// in addition to logfiles that are set up. +func SetlogToStdout(duplicate bool) { Config.lock.Lock() defer Config.lock.Unlock() - if format != "" { - Config.timestampFormat = format - } + Config.logToStdout = duplicate + reloadConfig() } func init() { Config = &logConfig{ - logfile: os.Getenv(LOGFILE_ENV_VAR), - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: setInstanceLevelDefault(), - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - timestampFormat: DEFAULT_TIMESTAMP_FORMAT, + logfile: os.Getenv(LOGFILE_ENV_VAR), + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: setInstanceLevelDefault(), + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } } +// InitSeelog registers custom logging formats, updates the internal Config struct +// and reloads the global logger. This should only be called once, as external +// callers should use the Config struct over environment variables directly. func InitSeelog() { if err := seelog.RegisterCustomFormatter("EcsAgentLogfmt", logfmtFormatter); err != nil { seelog.Error(err) @@ -303,8 +355,12 @@ func InitSeelog() { seelog.Error(err) } - SetLevel(os.Getenv(LOGLEVEL_ENV_VAR), os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR)) - + if DriverLogLevel := os.Getenv(LOGLEVEL_ENV_VAR); DriverLogLevel != "" { + SetDriverLogLevel(DriverLogLevel) + } + if InstanceLogLevel := os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR); InstanceLogLevel != "" { + SetInstanceLogLevel(InstanceLogLevel) + } if RolloverType := os.Getenv(LOG_ROLLOVER_TYPE_ENV_VAR); RolloverType != "" { Config.RolloverType = RolloverType } diff --git a/ecs-agent/logger/log.go b/ecs-agent/logger/log.go index fc7025f823c..4eb2a248543 100644 --- a/ecs-agent/logger/log.go +++ b/ecs-agent/logger/log.go @@ -44,18 +44,34 @@ const ( DEFAULT_TIMESTAMP_FORMAT = time.RFC3339 DEFAULT_MAX_FILE_SIZE float64 = 10 DEFAULT_MAX_ROLL_COUNT int = 24 + DEFAULT_LOGTO_STDOUT = true ) +// Because timestamp format will be called in the custom formatter +// for each log message processed, it should not be handled +// with an explicitly write protected configuration. +var timestampFormat = DEFAULT_TIMESTAMP_FORMAT + +// logLevels is the mapping from ECS_LOGLEVEL to Seelog provided levels. +var logLevels = map[string]string{ + "debug": "debug", + "info": "info", + "warn": "warn", + "error": "error", + "crit": "critical", + "none": "off", +} + type logConfig struct { - RolloverType string - MaxRollCount int - MaxFileSizeMB float64 - logfile string - driverLevel string - instanceLevel string - outputFormat string - timestampFormat string - lock sync.Mutex + RolloverType string + MaxRollCount int + MaxFileSizeMB float64 + logfile string + driverLevel string + instanceLevel string + outputFormat string + logToStdout bool + lock sync.Mutex } var Config *logConfig @@ -89,7 +105,7 @@ func logfmtFormatter(params string) seelog.FormatterFunc { buf.WriteString(level.String()) buf.WriteByte(' ') buf.WriteString("time=") - buf.WriteString(context.CallTime().UTC().Format(Config.timestampFormat)) + buf.WriteString(context.CallTime().UTC().Format(timestampFormat)) buf.WriteByte(' ') // temporary measure to make this change backwards compatible as we update to structured logs if strings.HasPrefix(message, structuredTxtFormatPrefix) { @@ -114,7 +130,7 @@ func jsonFormatter(params string) seelog.FormatterFunc { buf.WriteString(`{"level":"`) buf.WriteString(level.String()) buf.WriteString(`","time":"`) - buf.WriteString(context.CallTime().UTC().Format(Config.timestampFormat)) + buf.WriteString(context.CallTime().UTC().Format(timestampFormat)) buf.WriteString(`",`) // temporary measure to make this change backwards compatible as we update to structured logs if strings.HasPrefix(message, structuredJsonFormatPrefix) { @@ -144,14 +160,21 @@ func reloadConfig() { } func seelogConfig() string { + platformLogConfig := platformLogConfig() c := ` - + ` + if platformLogConfig != "" || Config.logToStdout { + c += ` - ` - c += platformLogConfig() - c += ` + ` + if Config.logToStdout { + c += ` ` + } + c += platformLogConfig + c += ` ` + } if Config.logfile != "" { c += ` ` @@ -159,10 +182,13 @@ func seelogConfig() string { c += ` ` - } else { + } else if Config.RolloverType == "date" { c += ` ` + } else { + c += ` + ` } c += ` ` @@ -191,33 +217,6 @@ func getLevelList(fileLevel string) string { return levelLists[fileLevel] } -// SetLevel sets the log levels for logging -func SetLevel(driverLogLevel, instanceLogLevel string) { - levels := map[string]string{ - "debug": "debug", - "info": "info", - "warn": "warn", - "error": "error", - "crit": "critical", - "none": "off", - } - - parsedDriverLevel, driverOk := levels[strings.ToLower(driverLogLevel)] - parsedInstanceLevel, instanceOk := levels[strings.ToLower(instanceLogLevel)] - - if instanceOk || driverOk { - Config.lock.Lock() - defer Config.lock.Unlock() - if instanceOk { - Config.instanceLevel = parsedInstanceLevel - } - if driverOk { - Config.driverLevel = parsedDriverLevel - } - reloadConfig() - } -} - // GetLevel gets the log level func GetLevel() string { Config.lock.Lock() @@ -236,12 +235,41 @@ func setInstanceLevelDefault() string { return DEFAULT_LOGLEVEL } +// SetInstanceLogLevel explicitly sets the log level for instance logs. +func SetInstanceLogLevel(instanceLogLevel string) { + parsedLevel, ok := logLevels[strings.ToLower(instanceLogLevel)] + if ok { + Config.lock.Lock() + defer Config.lock.Unlock() + Config.instanceLevel = parsedLevel + reloadConfig() + } else { + seelog.Error("Instance log level mapping not found") + } +} + +// SetDriverLogLevel explicitly sets the log level for a custom driver. +func SetDriverLogLevel(driverLogLevel string) { + parsedLevel, ok := logLevels[strings.ToLower(driverLogLevel)] + if ok { + Config.lock.Lock() + defer Config.lock.Unlock() + Config.driverLevel = parsedLevel + reloadConfig() + } else { + seelog.Error("Driver log level mapping not found") + } +} + // SetConfigLogFile sets the default output file of the logger. func SetConfigLogFile(logFile string) { - Config.lock.Lock() - defer Config.lock.Unlock() + if logFile != "" { + Config.lock.Lock() + defer Config.lock.Unlock() - Config.logfile = logFile + Config.logfile = logFile + reloadConfig() + } } // SetConfigLogFormat sets the output format of the logger. @@ -250,20 +278,33 @@ func SetConfigLogFormat(logFormat string) { Config.lock.Lock() defer Config.lock.Unlock() - os.Setenv(LOG_OUTPUT_FORMAT_ENV_VAR, logFormat) Config.outputFormat = logFormat + reloadConfig() } // SetConfigMaxFileSizeMB sets the max file size of a log file // in Megabytes before the logger rotates to a new file. func SetConfigMaxFileSizeMB(maxSizeInMB float64) { - Config.lock.Lock() - defer Config.lock.Unlock() + if maxSizeInMB > 0 { + Config.lock.Lock() + defer Config.lock.Unlock() - // Parse unit as string to set as environment variable. - strsize := strconv.FormatFloat(maxSizeInMB, 'f', -1, 64) - os.Setenv(LOG_MAX_FILE_SIZE_ENV_VAR, strsize) - Config.MaxFileSizeMB = maxSizeInMB + Config.MaxFileSizeMB = maxSizeInMB + reloadConfig() + } +} + +// SetRolloverType sets the logging rollover constraint. +// This should be either size or date. Logger will roll +// to a new log file based on this constraint. +func SetRolloverType(rolloverType string) { + if rolloverType == "date" || rolloverType == "size" { + Config.lock.Lock() + defer Config.lock.Unlock() + + Config.RolloverType = rolloverType + reloadConfig() + } } // SetTimestampFormat sets the time formatting @@ -271,27 +312,38 @@ func SetConfigMaxFileSizeMB(maxSizeInMB float64) { // a valid time format such as time.RFC3339 // or "2006-01-02T15:04:05.000". func SetTimestampFormat(format string) { + if format != "" { + timestampFormat = format + } +} + +// SetlogToStdout decides whether the logger +// should write to stdout using the tag +// in addition to logfiles that are set up. +func SetlogToStdout(duplicate bool) { Config.lock.Lock() defer Config.lock.Unlock() - if format != "" { - Config.timestampFormat = format - } + Config.logToStdout = duplicate + reloadConfig() } func init() { Config = &logConfig{ - logfile: os.Getenv(LOGFILE_ENV_VAR), - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: setInstanceLevelDefault(), - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - timestampFormat: DEFAULT_TIMESTAMP_FORMAT, + logfile: os.Getenv(LOGFILE_ENV_VAR), + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: setInstanceLevelDefault(), + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } } +// InitSeelog registers custom logging formats, updates the internal Config struct +// and reloads the global logger. This should only be called once, as external +// callers should use the Config struct over environment variables directly. func InitSeelog() { if err := seelog.RegisterCustomFormatter("EcsAgentLogfmt", logfmtFormatter); err != nil { seelog.Error(err) @@ -303,8 +355,12 @@ func InitSeelog() { seelog.Error(err) } - SetLevel(os.Getenv(LOGLEVEL_ENV_VAR), os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR)) - + if DriverLogLevel := os.Getenv(LOGLEVEL_ENV_VAR); DriverLogLevel != "" { + SetDriverLogLevel(DriverLogLevel) + } + if InstanceLogLevel := os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR); InstanceLogLevel != "" { + SetInstanceLogLevel(InstanceLogLevel) + } if RolloverType := os.Getenv(LOG_ROLLOVER_TYPE_ENV_VAR); RolloverType != "" { Config.RolloverType = RolloverType } diff --git a/ecs-agent/logger/log_test.go b/ecs-agent/logger/log_test.go index 2881a0b9b34..a41ddbfbf12 100644 --- a/ecs-agent/logger/log_test.go +++ b/ecs-agent/logger/log_test.go @@ -142,7 +142,7 @@ func TestJSONFormat_Structured_Timestamp(t *testing.T) { require.JSONEq(t, `{"level": "debug", "time": "2018-10-01T01:02:03.000", "msg": "This is my log message"}`, s) } -func TestSetLevel(t *testing.T) { +func TestSetLogLevels(t *testing.T) { resetEnv := func() { os.Unsetenv(LOGLEVEL_ENV_VAR) os.Unsetenv(LOGLEVEL_ON_INSTANCE_ENV_VAR) @@ -241,8 +241,11 @@ func TestSetLevel(t *testing.T) { MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, } - SetLevel(os.Getenv(LOGLEVEL_ENV_VAR), os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR)) + + SetDriverLogLevel(test.loglevel) require.Equal(t, test.expectedLoglevel, Config.driverLevel) + + SetInstanceLogLevel(test.loglevelInstance) require.Equal(t, test.expectedLoglevelInstance, Config.instanceLevel) }) } @@ -291,3 +294,11 @@ func (l *LogContextMock) CallTime() time.Time { func (l *LogContextMock) CustomContext() interface{} { return map[string]string{} } + +func updateLogConfig(config *logConfig) string { + mutex := Config.lock + mutex.Lock() + Config = config + mutex.Unlock() + return seelogConfig() +} diff --git a/ecs-agent/logger/log_unix_test.go b/ecs-agent/logger/log_unix_test.go index 0b063d1837b..cee54520fb2 100644 --- a/ecs-agent/logger/log_unix_test.go +++ b/ecs-agent/logger/log_unix_test.go @@ -32,6 +32,7 @@ func TestSeelogConfig_Default(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -61,6 +62,7 @@ func TestSeelogConfig_WithoutLogFile(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -78,6 +80,33 @@ func TestSeelogConfig_WithoutLogFile(t *testing.T) { `, c) } +func TestSeeLogConfig_WithoutStdout(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: "none", + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: false, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + +`, c) +} + func TestSeelogConfig_DebugLevel(t *testing.T) { Config = &logConfig{ logfile: "foo.log", @@ -87,6 +116,7 @@ func TestSeelogConfig_DebugLevel(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -117,6 +147,7 @@ func TestSeelogConfig_SizeRollover(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -147,6 +178,7 @@ func TestSeelogConfig_SizeRolloverFileSizeChange(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: 15, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -177,6 +209,7 @@ func TestSeelogConfig_SizeRolloverRollCountChange(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: 15, MaxRollCount: 10, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -207,6 +240,7 @@ func TestSeelogConfig_JSONOutput(t *testing.T) { outputFormat: "json", MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: 10, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -228,6 +262,64 @@ func TestSeelogConfig_JSONOutput(t *testing.T) { `, c) } +func TestSeelogConfig_JSONNoStdout(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: "json", + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: 10, + logToStdout: false, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_JSONNoRollover(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: "none", + outputFormat: "json", + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: 10, + logToStdout: DEFAULT_LOGTO_STDOUT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + func TestSeelogConfig_NoOnInstanceLog(t *testing.T) { Config = &logConfig{ logfile: "foo.log", @@ -237,6 +329,7 @@ func TestSeelogConfig_NoOnInstanceLog(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -267,6 +360,7 @@ func TestSeelogConfig_DifferentLevels(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -300,6 +394,7 @@ func TestSeelogConfig_FileLevelDefault(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` diff --git a/ecs-agent/logger/log_windows_test.go b/ecs-agent/logger/log_windows_test.go index c763d595ac8..8dcd5fe4369 100644 --- a/ecs-agent/logger/log_windows_test.go +++ b/ecs-agent/logger/log_windows_test.go @@ -32,6 +32,7 @@ func TestSeelogConfigWindows_Default(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -62,6 +63,7 @@ func TestSeelogConfigWindows_WithoutLogFile(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -89,6 +91,7 @@ func TestSeelogConfigWindows_DebugLevel(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -120,6 +123,7 @@ func TestSeelogConfigWindows_SizeRollover(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -151,6 +155,7 @@ func TestSeelogConfigWindows_SizeRolloverFileSizeChange(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: 15, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -182,6 +187,7 @@ func TestSeelogConfigWindows_SizeRolloverRollCountChange(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: 15, MaxRollCount: 10, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -213,6 +219,7 @@ func TestSeelogConfigWindows_JSONOutput(t *testing.T) { outputFormat: "json", MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: 10, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -244,6 +251,7 @@ func TestSeelogConfigWindows_NoOnInstanceLog(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -275,6 +283,7 @@ func TestSeelogConfigWindows_DifferentLevels(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, ` @@ -309,6 +318,7 @@ func TestSeelogConfigWindows_FileLevelDefault(t *testing.T) { outputFormat: DEFAULT_OUTPUT_FORMAT, MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + logToStdout: DEFAULT_LOGTO_STDOUT, } c := seelogConfig() require.Equal(t, `