Skip to content

Commit

Permalink
Merge pull request #14 from anchore/feat/remove-dev-logging
Browse files Browse the repository at this point in the history
feat: log timestamps as iso, remove dev logging
  • Loading branch information
bradleyjones committed Jan 5, 2023
2 parents 18870a3 + ac19518 commit 370ce0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
1 change: 0 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func initLogging() {
logConfig := logger.LogConfig{
Level: appConfig.Log.Level,
FileLocation: appConfig.Log.FileLocation,
Dev: appConfig.Dev.Log,
}

logger.InitLogger(logConfig)
Expand Down
1 change: 0 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type Logging struct {
// Development Configuration (only profile-cpu at the moment)
type Development struct {
ProfileCPU bool `mapstructure:"profile-cpu"`
Log bool `mapstructure:"log"`
}

// Return whether or not AnchoreDetails are specified
Expand Down
32 changes: 12 additions & 20 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (log Logger) Error(msg string, err error, args ...interface{}) {
type LogConfig struct {
Level string
FileLocation string
Dev bool
}

var Log Logger
Expand All @@ -47,31 +46,24 @@ func InitLogger(logConfig LogConfig) {
level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
}

if logConfig.Dev {
if logConfig.FileLocation != "" {
cfg = zap.Config{
Level: level,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{logConfig.FileLocation},
}
} else {
zapEncoderCfg := zap.NewProductionEncoderConfig()
zapEncoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder

cfg = zap.Config{
Level: level,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
EncoderConfig: zapEncoderCfg,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
} else {
if logConfig.FileLocation != "" {
cfg = zap.Config{
Level: level,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{logConfig.FileLocation},
}
} else {
cfg = zap.Config{
Level: level,
Encoding: "console",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}
}

Log = Logger{
Expand Down
6 changes: 3 additions & 3 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestLoggerInit(t *testing.T) {
assert.Nil(t, Log.zap)

InitLogger(LogConfig{Level: "info", FileLocation: "", Dev: false})
InitLogger(LogConfig{Level: "info", FileLocation: ""})

assert.NotNil(t, Log.zap)
}
Expand All @@ -20,7 +20,7 @@ func TestLogsToFileIfFileLocationProvided(t *testing.T) {
tmpDir := t.TempDir()
fileLocation := path.Join(tmpDir, "log")

InitLogger(LogConfig{Level: "info", FileLocation: fileLocation, Dev: false})
InitLogger(LogConfig{Level: "info", FileLocation: fileLocation})

var expectedLogMsg = "test log foobar"
Log.Info(expectedLogMsg)
Expand All @@ -35,7 +35,7 @@ func TestLogsToFileIfFileLocationProvided(t *testing.T) {
}

func TestLoggerDefaultsToInfoLevelOnInvalidLevel(t *testing.T) {
InitLogger(LogConfig{Level: "invalid", FileLocation: "", Dev: false})
InitLogger(LogConfig{Level: "invalid", FileLocation: ""})

assert.Equal(t, Log.zap.Level().String(), "info")
}

0 comments on commit 370ce0e

Please sign in to comment.