From d7687578f8f3ea0d0515f8de121ab9af618887db Mon Sep 17 00:00:00 2001 From: Teresa Amor Date: Fri, 28 Aug 2020 10:58:22 -0700 Subject: [PATCH] logger: alter loglevel behavior --- README.md | 4 +- agent/logger/log.go | 3 + agent/logger/log_test.go | 406 ++++++++----------------------- agent/logger/log_unix_test.go | 296 ++++++++++++++++++++++ agent/logger/log_windows_test.go | 144 +---------- 5 files changed, 409 insertions(+), 444 deletions(-) create mode 100644 agent/logger/log_unix_test.go diff --git a/README.md b/README.md index e45b939b54b..3c6905ed49d 100644 --- a/README.md +++ b/README.md @@ -133,8 +133,8 @@ additional details on each available environment variable. | `AWS_SECRET_ACCESS_KEY` | EXAMPLEKEY | The [secret key](http://docs.aws.amazon.com/general/latest/gr/aws-security-credentials.html) used by the agent for all calls. | Taken from Amazon EC2 instance metadata. | Taken from Amazon EC2 instance metadata. | | `AWS_SESSION_TOKEN` | | The [session token](http://docs.aws.amazon.com/STS/latest/UsingSTS/Welcome.html) used for temporary credentials. | Taken from Amazon EC2 instance metadata. | Taken from Amazon EC2 instance metadata. | | `DOCKER_HOST` | `unix:///var/run/docker.sock` | Used to create a connection to the Docker daemon; behaves similarly to this environment variable as used by the Docker client. | `unix:///var/run/docker.sock` | `npipe:////./pipe/docker_engine` | -| `ECS_LOGLEVEL` | <crit> | <error> | <warn> | <info> | <debug> | The level of detail that should be logged by the logging driver, or on Windows, the Windows Event Log. | info | info | -| `ECS_LOGLEVEL_ON_INSTANCE` | <none> | <crit> | <error> | <warn> | <info> | <debug> | The level of detail that should be logged in the on-instance log file. | none if `ECS_LOG_DRIVER` is explicitly set to a non-empty value; info otherwise | none if `ECS_LOG_DRIVER` is explicitly set to a non-empty value; info otherwise | +| `ECS_LOGLEVEL` | <crit> | <error> | <warn> | <info> | <debug> | The level of detail to be logged. | info | info | +| `ECS_LOGLEVEL_ON_INSTANCE` | <none> | <crit> | <error> | <warn> | <info> | <debug> | Can be used to override `ECS_LOGLEVEL` and set a level of detail that should be logged in the on-instance log file, separate from the level that is logged in the logging driver. If a logging driver is explicitly set, on-instance logs are turned off by default, but can be turned back on with this variable. | none if `ECS_LOG_DRIVER` is explicitly set to a non-empty value; otherwise the same value as `ECS_LOGLEVEL` | none if `ECS_LOG_DRIVER` is explicitly set to a non-empty value; otherwise the same value as `ECS_LOGLEVEL` | | `ECS_LOGFILE` | /ecs-agent.log | The location where logs should be written. Log level is controlled by `ECS_LOGLEVEL`. | blank | blank | | `ECS_CHECKPOINT` | <true | false> | Whether to checkpoint state to the DATADIR specified below. | true if `ECS_DATADIR` is explicitly set to a non-empty value; false otherwise | true if `ECS_DATADIR` is explicitly set to a non-empty value; false otherwise | | `ECS_DATADIR` | /data/ | The container path where state is checkpointed for use across agent restarts. Note that on Linux, when you specify this, you will need to make sure that the Agent container has a bind mount of `$ECS_HOST_DATA_DIR/data:$ECS_DATADIR` with the corresponding values of `ECS_HOST_DATA_DIR` and `ECS_DATADIR`. | /data/ | `C:\ProgramData\Amazon\ECS\data` diff --git a/agent/logger/log.go b/agent/logger/log.go index 39861e4155e..75899c2fb2f 100644 --- a/agent/logger/log.go +++ b/agent/logger/log.go @@ -163,6 +163,9 @@ func setInstanceLevelDefault() string { if logDriver := os.Getenv(LOG_DRIVER_ENV_VAR); logDriver != "" { return DEFAULT_LOGLEVEL_WHEN_DRIVER_SET } + if loglevel := os.Getenv(LOGLEVEL_ENV_VAR); loglevel != "" { + return loglevel + } return DEFAULT_LOGLEVEL } diff --git a/agent/logger/log_test.go b/agent/logger/log_test.go index a2233e57934..d0c6d97d091 100644 --- a/agent/logger/log_test.go +++ b/agent/logger/log_test.go @@ -1,4 +1,4 @@ -// +build !windows +// +build unit // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // @@ -58,320 +58,110 @@ func TestJSONFormat_debug(t *testing.T) { require.JSONEq(t, `{"level": "debug", "time": "2018-10-01T01:02:03Z", "msg": "This is my log message", "module": "mytestmodule.go"}`, s) } -func TestSeelogConfig_Default(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: DEFAULT_LOGLEVEL, - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_DebugLevel(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: "debug", - instanceLevel: DEFAULT_LOGLEVEL, - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_SizeRollover(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: DEFAULT_LOGLEVEL, - RolloverType: "size", - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_SizeRolloverFileSizeChange(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: DEFAULT_LOGLEVEL, - RolloverType: "size", - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: 15, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_SizeRolloverRollCountChange(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: DEFAULT_LOGLEVEL, - RolloverType: "size", - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: 15, - MaxRollCount: 10, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_JSONOutput(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, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_NoOnInstanceLog(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: DEFAULT_LOGLEVEL_WHEN_DRIVER_SET, - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_DifferentLevels(t *testing.T) { - Config = &logConfig{ - logfile: "foo.log", - driverLevel: "warn", - instanceLevel: "critical", - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - -func TestSeelogConfig_FileLevelDefault(t *testing.T) { - os.Setenv(LOG_DRIVER_ENV_VAR, "awslogs") - defer os.Unsetenv(LOG_DRIVER_ENV_VAR) - - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: setInstanceLevelDefault(), - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) -} - func TestSetLevel(t *testing.T) { - os.Setenv(LOGLEVEL_ON_INSTANCE_ENV_VAR, "crit") - os.Setenv(LOGLEVEL_ENV_VAR, "debug") - resetEnv := func() { os.Unsetenv(LOGLEVEL_ENV_VAR) os.Unsetenv(LOGLEVEL_ON_INSTANCE_ENV_VAR) + os.Unsetenv(LOG_DRIVER_ENV_VAR) } - defer resetEnv() - - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: setInstanceLevelDefault(), - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + resetEnv() + + testcases := []struct { + name string + logDriver string + loglevel string + loglevelInstance string + expectedLoglevel string + expectedLoglevelInstance string + }{ + { + name: "nothing set", + logDriver: "", + loglevel: "", + loglevelInstance: "", + expectedLoglevel: "info", + expectedLoglevelInstance: "info", + }, + { + name: "only loglevel", + logDriver: "", + loglevel: "debug", + loglevelInstance: "", + expectedLoglevel: "debug", + expectedLoglevelInstance: "debug", + }, + { + name: "only on instance", + logDriver: "", + loglevel: "", + loglevelInstance: "debug", + expectedLoglevel: "info", + expectedLoglevelInstance: "debug", + }, + { + name: "both levels no driver", + logDriver: "", + loglevel: "warn", + loglevelInstance: "crit", + expectedLoglevel: "warn", + expectedLoglevelInstance: "critical", + }, + { + name: "loglevel and driver", + logDriver: "journald", + loglevel: "debug", + loglevelInstance: "", + expectedLoglevel: "debug", + expectedLoglevelInstance: "off", + }, + { + name: "loglevel on instance and driver", + logDriver: "journald", + loglevel: "", + loglevelInstance: "debug", + expectedLoglevel: "info", + expectedLoglevelInstance: "debug", + }, + { + name: "both levels and driver", + logDriver: "journald", + loglevel: "warn", + loglevelInstance: "debug", + expectedLoglevel: "warn", + expectedLoglevelInstance: "debug", + }, + { + name: "only driver", + logDriver: "journald", + loglevel: "", + loglevelInstance: "", + expectedLoglevel: "info", + expectedLoglevelInstance: "off", + }, } - SetLevel(os.Getenv(LOGLEVEL_ENV_VAR), os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR)) - require.Equal(t, "debug", Config.driverLevel) - require.Equal(t, "critical", Config.instanceLevel) - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - -`, c) + for _, test := range testcases { + t.Run(test.name, func(t *testing.T) { + defer resetEnv() + + os.Setenv(LOGLEVEL_ENV_VAR, test.loglevel) + os.Setenv(LOGLEVEL_ON_INSTANCE_ENV_VAR, test.loglevelInstance) + os.Setenv(LOG_DRIVER_ENV_VAR, test.logDriver) + + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: setInstanceLevelDefault(), + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + SetLevel(os.Getenv(LOGLEVEL_ENV_VAR), os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR)) + require.Equal(t, test.expectedLoglevel, Config.driverLevel) + require.Equal(t, test.expectedLoglevelInstance, Config.instanceLevel) + }) + } } type LogContextMock struct{} diff --git a/agent/logger/log_unix_test.go b/agent/logger/log_unix_test.go new file mode 100644 index 00000000000..10455f256cf --- /dev/null +++ b/agent/logger/log_unix_test.go @@ -0,0 +1,296 @@ +// +build !windows,unit + +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package logger + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSeelogConfig_Default(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_DebugLevel(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: "debug", + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_SizeRollover(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: "size", + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_SizeRolloverFileSizeChange(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: "size", + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: 15, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_SizeRolloverRollCountChange(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL, + RolloverType: "size", + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: 15, + MaxRollCount: 10, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_JSONOutput(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, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_NoOnInstanceLog(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: DEFAULT_LOGLEVEL_WHEN_DRIVER_SET, + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_DifferentLevels(t *testing.T) { + Config = &logConfig{ + logfile: "foo.log", + driverLevel: "warn", + instanceLevel: "critical", + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} + +func TestSeelogConfig_FileLevelDefault(t *testing.T) { + os.Setenv(LOG_DRIVER_ENV_VAR, "awslogs") + defer os.Unsetenv(LOG_DRIVER_ENV_VAR) + + Config = &logConfig{ + logfile: "foo.log", + driverLevel: DEFAULT_LOGLEVEL, + instanceLevel: setInstanceLevelDefault(), + RolloverType: DEFAULT_ROLLOVER_TYPE, + outputFormat: DEFAULT_OUTPUT_FORMAT, + MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, + MaxRollCount: DEFAULT_MAX_ROLL_COUNT, + } + c := seelogConfig() + require.Equal(t, ` + + + + + + + + + + + + + + +`, c) +} diff --git a/agent/logger/log_windows_test.go b/agent/logger/log_windows_test.go index f94655d9c15..608b55d3103 100644 --- a/agent/logger/log_windows_test.go +++ b/agent/logger/log_windows_test.go @@ -1,4 +1,4 @@ -// +build windows +// +build windows,unit // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // @@ -18,47 +18,11 @@ package logger import ( "os" "testing" - "time" - "github.com/cihub/seelog" "github.com/stretchr/testify/require" ) -func TestLogfmtFormat(t *testing.T) { - logfmt := logfmtFormatter("") - out := logfmt("This is my log message", seelog.InfoLvl, &LogContextMock{}) - s, ok := out.(string) - require.True(t, ok) - require.Equal(t, `level=info time=2018-10-01T01:02:03Z msg="This is my log message" module=mytestmodule.go -`, s) -} - -func TestJSONFormat(t *testing.T) { - jsonF := jsonFormatter("") - out := jsonF("This is my log message", seelog.InfoLvl, &LogContextMock{}) - s, ok := out.(string) - require.True(t, ok) - require.JSONEq(t, `{"level": "info", "time": "2018-10-01T01:02:03Z", "msg": "This is my log message", "module": "mytestmodule.go"}`, s) -} - -func TestLogfmtFormat_debug(t *testing.T) { - logfmt := logfmtFormatter("") - out := logfmt("This is my log message", seelog.DebugLvl, &LogContextMock{}) - s, ok := out.(string) - require.True(t, ok) - require.Equal(t, `level=debug time=2018-10-01T01:02:03Z msg="This is my log message" module=mytestmodule.go -`, s) -} - -func TestJSONFormat_debug(t *testing.T) { - jsonF := jsonFormatter("") - out := jsonF("This is my log message", seelog.DebugLvl, &LogContextMock{}) - s, ok := out.(string) - require.True(t, ok) - require.JSONEq(t, `{"level": "debug", "time": "2018-10-01T01:02:03Z", "msg": "This is my log message", "module": "mytestmodule.go"}`, s) -} - -func TestSeelogConfig_Default(t *testing.T) { +func TestSeelogConfigWindows_Default(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: DEFAULT_LOGLEVEL, @@ -89,7 +53,7 @@ func TestSeelogConfig_Default(t *testing.T) { `, c) } -func TestSeelogConfig_DebugLevel(t *testing.T) { +func TestSeelogConfigWindows_DebugLevel(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: "debug", @@ -120,7 +84,7 @@ func TestSeelogConfig_DebugLevel(t *testing.T) { `, c) } -func TestSeelogConfig_SizeRollover(t *testing.T) { +func TestSeelogConfigWindows_SizeRollover(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: DEFAULT_LOGLEVEL, @@ -151,7 +115,7 @@ func TestSeelogConfig_SizeRollover(t *testing.T) { `, c) } -func TestSeelogConfig_SizeRolloverFileSizeChange(t *testing.T) { +func TestSeelogConfigWindows_SizeRolloverFileSizeChange(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: DEFAULT_LOGLEVEL, @@ -182,7 +146,7 @@ func TestSeelogConfig_SizeRolloverFileSizeChange(t *testing.T) { `, c) } -func TestSeelogConfig_SizeRolloverRollCountChange(t *testing.T) { +func TestSeelogConfigWindows_SizeRolloverRollCountChange(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: DEFAULT_LOGLEVEL, @@ -213,7 +177,7 @@ func TestSeelogConfig_SizeRolloverRollCountChange(t *testing.T) { `, c) } -func TestSeelogConfig_JSONOutput(t *testing.T) { +func TestSeelogConfigWindows_JSONOutput(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: DEFAULT_LOGLEVEL, @@ -244,7 +208,7 @@ func TestSeelogConfig_JSONOutput(t *testing.T) { `, c) } -func TestSeelogConfig_NoOnInstanceLog(t *testing.T) { +func TestSeelogConfigWindows_NoOnInstanceLog(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: DEFAULT_LOGLEVEL, @@ -275,7 +239,7 @@ func TestSeelogConfig_NoOnInstanceLog(t *testing.T) { `, c) } -func TestSeelogConfig_DifferentLevels(t *testing.T) { +func TestSeelogConfigWindows_DifferentLevels(t *testing.T) { Config = &logConfig{ logfile: "foo.log", driverLevel: "warn", @@ -306,7 +270,7 @@ func TestSeelogConfig_DifferentLevels(t *testing.T) { `, c) } -func TestSeelogConfig_FileLevelDefault(t *testing.T) { +func TestSeelogConfigWindows_FileLevelDefault(t *testing.T) { os.Setenv(LOG_DRIVER_ENV_VAR, "awslogs") defer os.Unsetenv(LOG_DRIVER_ENV_VAR) @@ -339,91 +303,3 @@ func TestSeelogConfig_FileLevelDefault(t *testing.T) { `, c) } - -func TestSetLevel(t *testing.T) { - os.Setenv(LOGLEVEL_ON_INSTANCE_ENV_VAR, "crit") - os.Setenv(LOGLEVEL_ENV_VAR, "debug") - - resetEnv := func() { - os.Unsetenv(LOGLEVEL_ENV_VAR) - os.Unsetenv(LOGLEVEL_ON_INSTANCE_ENV_VAR) - } - defer resetEnv() - - Config = &logConfig{ - logfile: "foo.log", - driverLevel: DEFAULT_LOGLEVEL, - instanceLevel: setInstanceLevelDefault(), - RolloverType: DEFAULT_ROLLOVER_TYPE, - outputFormat: DEFAULT_OUTPUT_FORMAT, - MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, - MaxRollCount: DEFAULT_MAX_ROLL_COUNT, - } - SetLevel(os.Getenv(LOGLEVEL_ENV_VAR), os.Getenv(LOGLEVEL_ON_INSTANCE_ENV_VAR)) - require.Equal(t, "debug", Config.driverLevel) - require.Equal(t, "critical", Config.instanceLevel) - - c := seelogConfig() - require.Equal(t, ` - - - - - - - - - - - - - - - -`, c) -} - -type LogContextMock struct{} - -// Caller's function name. -func (l *LogContextMock) Func() string { - return "" -} - -// Caller's line number. -func (l *LogContextMock) Line() int { - return 0 -} - -// Caller's file short path (in slashed form). -func (l *LogContextMock) ShortPath() string { - return "" -} - -// Caller's file full path (in slashed form). -func (l *LogContextMock) FullPath() string { - return "" -} - -// Caller's file name (without path). -func (l *LogContextMock) FileName() string { - return "mytestmodule.go" -} - -// True if the context is correct and may be used. -// If false, then an error in context evaluation occurred and -// all its other data may be corrupted. -func (l *LogContextMock) IsValid() bool { - return true -} - -// Time when log function was called. -func (l *LogContextMock) CallTime() time.Time { - return time.Date(2018, time.October, 1, 1, 2, 3, 0, time.UTC) -} - -// Custom context that can be set by calling logger.SetContext -func (l *LogContextMock) CustomContext() interface{} { - return map[string]string{} -}