Skip to content

Commit

Permalink
fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nithu0115 authored and jayanthvn committed Aug 14, 2020
1 parent 8e61bec commit 723d18c
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 24 deletions.
5 changes: 2 additions & 3 deletions cmd/aws-k8s-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
)

const binaryName = "ipamd"

var version string

func main() {
Expand All @@ -33,7 +31,8 @@ func main() {
func _main() int {
//Do not add anything before initializing logger
logConfig := logger.Configuration{
BinaryName: binaryName,
LogLevel: logger.GetLogLevel(),
LogLocation: logger.GetLogLocation(),
}
log := logger.New(&logConfig)

Expand Down
3 changes: 0 additions & 3 deletions cmd/cni-metrics-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"github.com/aws/amazon-vpc-cni-k8s/pkg/publisher"
)

const binaryName = "cni-metrics-helper"

type options struct {
submitCW bool
help bool
Expand All @@ -40,7 +38,6 @@ func main() {
// Do not add anything before initializing logger
logLevel := logger.GetLogLevel()
logConfig := logger.Configuration{
BinaryName: binaryName,
LogLevel: logLevel,
LogLocation: "stdout",
}
Expand Down
1 change: 0 additions & 1 deletion cmd/routed-eni-cni-plugin/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func LoadNetConf(bytes []byte) (*NetConf, logger.Logger, error) {

//logConfig
logConfig := logger.Configuration{
BinaryName: conf.Name,
LogLevel: conf.PluginLogLevel,
LogLocation: conf.PluginLogFile,
}
Expand Down
1 change: 0 additions & 1 deletion cmd/routed-eni-cni-plugin/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const (
)

var logConfig = logger.Configuration{
BinaryName: "aws-cni",
LogLevel: "Debug",
LogLocation: "stdout",
}
Expand Down
1 change: 0 additions & 1 deletion pkg/ipamd/datastore/data_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

var logConfig = logger.Configuration{
BinaryName: "aws-cni",
LogLevel: "Debug",
LogLocation: "stdout",
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/utils/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,25 @@ import (
const (
defaultLogFilePath = "/host/var/log/aws-routed-eni/ipamd.log"
defaultLogLevel = "Debug"
binaryName = "L-IPamD"
envLogLevel = "AWS_VPC_K8S_CNI_LOGLEVEL"
envLogFilePath = "AWS_VPC_K8S_CNI_LOG_FILE"
)

// Configuration stores the config for the logger
type Configuration struct {
BinaryName string
LogLevel string
LogLocation string
}

func LoadLogConfig() *Configuration {
return &Configuration{
BinaryName: binaryName,
LogLevel: GetLogLevel(),
LogLocation: getLogFileLocation(),
LogLocation: GetLogLocation(),
}
}

// GetLogFileLocation returns the log file path
func getLogFileLocation() string {
func GetLogLocation() string {
logFilePath := os.Getenv(envLogFilePath)
if logFilePath == "" {
logFilePath = defaultLogFilePath
Expand Down
7 changes: 0 additions & 7 deletions pkg/utils/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package logger

const ipamdBinaryName = "aws-k8s-agent"

//Log is global variable so that log functions can be directly accessed
var log Logger

Expand Down Expand Up @@ -64,11 +62,6 @@ func (logf *structuredLogger) isEmpty() bool {

//New logger initializes logger
func New(inputLogConfig *Configuration) Logger {
if inputLogConfig.BinaryName == ipamdBinaryName {
logConfig := LoadLogConfig()
log = logConfig.newZapLogger()
return log
}
log = inputLogConfig.newZapLogger()
return log
}
4 changes: 2 additions & 2 deletions pkg/utils/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestEnvLogFilePath(t *testing.T) {
_ = os.Setenv(envLogFilePath, path)
defer os.Unsetenv(envLogFilePath)

assert.Equal(t, path, getLogFileLocation())
assert.Equal(t, path, GetLogLocation())
}

func TestGetLogFileLocationReturnsDefaultPath(t *testing.T) {
defaultPath := "/host/var/log/aws-routed-eni/ipamd.log"
assert.Equal(t, defaultPath, getLogFileLocation())
assert.Equal(t, defaultPath, GetLogLocation())
}

func TestLogLevelReturnsOverriddenLevel(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/logger/zaplogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/natefinch/lumberjack.v2"
)

type structuredLogger struct {
Expand Down

0 comments on commit 723d18c

Please sign in to comment.