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

Output to stderr when no log file path is passed #1365

Merged
merged 1 commit into from
Feb 1, 2021
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ Default: `/var/log/aws-routed-eni/plugin.log`
Valid Values: `stderr` or a file path

Specifies where to write the logging output for `aws-cni` plugin. Either to `stderr` or to override the default file (i.e., `/var/log/aws-routed-eni/plugin.log`).
`Stdout` cannot be supported for plugin log, please refer to #1248 for more details.
`Stdout` cannot be supported for plugin log, please refer to [#1248](https://github.com/aws/amazon-vpc-cni-k8s/issues/1248) for more details.

Note: If chaining an external plugin (i.e Cilium) that does not provide a `pluginLogFile` in its config file, the CNI plugin will by default write to `os.Stderr`. The output of `cmdAdd` are available in the Kubelet logs.

---

Expand Down
25 changes: 25 additions & 0 deletions pkg/utils/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

func TestEnvLogFilePath(t *testing.T) {
Expand Down Expand Up @@ -58,3 +59,27 @@ func TestLogLevelReturnsDefaultLevelWhenEnvSetToInvalidValue(t *testing.T) {
expectedLogLevel = zapcore.DebugLevel
assert.Equal(t, expectedLogLevel, getZapLevel(inputLogLevel))
}

func TestGetPluginLogFilePathEmpty(t *testing.T) {
expectedWriter := zapcore.Lock(os.Stderr)
inputPluginLogFile := ""
assert.Equal(t, expectedWriter, getPluginLogFilePath(inputPluginLogFile))
}

func TestGetPluginLogFilePathStdout(t *testing.T) {
expectedWriter := zapcore.Lock(os.Stdout)
inputPluginLogFile := "stdout"
assert.Equal(t, expectedWriter, getPluginLogFilePath(inputPluginLogFile))
}

func TestGetPluginLogFilePath(t *testing.T) {
inputPluginLogFile := "/var/log/aws-routed-eni/plugin.log"
expectedLumberJackLogger := &lumberjack.Logger{
Filename: "/var/log/aws-routed-eni/plugin.log",
MaxSize: 100,
MaxBackups: 5,
MaxAge: 30,
Compress: true,
}
assert.Equal(t, zapcore.AddSync(expectedLumberJackLogger), getPluginLogFilePath(inputPluginLogFile))
}
23 changes: 16 additions & 7 deletions pkg/utils/logger/zaplogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,11 @@ func getEncoder() zapcore.Encoder {

func (logConfig *Configuration) newZapLogger() *structuredLogger {
var cores []zapcore.Core
var writer zapcore.WriteSyncer

logLevel := getZapLevel(logConfig.LogLevel)

logFilePath := logConfig.LogLocation
writer := getPluginLogFilePath(logConfig.LogLocation)

if logFilePath != "" && strings.ToLower(logFilePath) != "stdout" {
writer = getLogWriter(logFilePath)
} else {
writer = zapcore.Lock(os.Stdout)
}
cores = append(cores, zapcore.NewCore(getEncoder(), writer, logLevel))

combinedCore := zapcore.NewTee(cores...)
Expand All @@ -132,6 +126,21 @@ func (logConfig *Configuration) newZapLogger() *structuredLogger {
}
}

// getPluginLogFilePath returns the writer
func getPluginLogFilePath(logFilePath string) zapcore.WriteSyncer {
var writer zapcore.WriteSyncer

if logFilePath == "" {
writer = zapcore.Lock(os.Stderr)
} else if strings.ToLower(logFilePath) != "stdout" {
writer = getLogWriter(logFilePath)
} else {
writer = zapcore.Lock(os.Stdout)
}

return writer
}

//getLogWriter is for lumberjack
func getLogWriter(logFilePath string) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Expand Down