-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2855b16
commit d276898
Showing
16 changed files
with
259 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,33 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
"time" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
once sync.Once //nolint:gochecknoglobals | ||
logger *zerolog.Logger //nolint:gochecknoglobals | ||
) | ||
|
||
// humanConsoleWriter configures the human-readable output. | ||
var humanConsoleWriter = zerolog.ConsoleWriter{ //nolint:gochecknoglobals | ||
Out: os.Stdout, | ||
TimeFormat: time.RFC3339, | ||
} | ||
// NewLogger initializes the Logger with the given arguments. | ||
func NewLogger(logLevel string, output string) (*zap.Logger, error) { | ||
var err error | ||
var loggerConfig zap.Config | ||
|
||
// Logger returns an instance of Logger. | ||
func Logger(logLevel string, output string) *zerolog.Logger { | ||
if logger == nil { | ||
once.Do( | ||
func() { | ||
logger = newLogger(logLevel, output) | ||
}) | ||
switch output { | ||
case "json": | ||
loggerConfig = zap.NewProductionConfig() | ||
case "human": | ||
loggerConfig = zap.NewDevelopmentConfig() | ||
default: | ||
return nil, fmt.Errorf("invalid logger output: %q", output) | ||
} | ||
|
||
return logger | ||
} | ||
|
||
// newLogger initializes the Logger with the given arguments. | ||
func newLogger(logLevel string, output string) *zerolog.Logger { | ||
level, err := zerolog.ParseLevel(logLevel) | ||
loggerConfig.Level, err = zap.ParseAtomicLevel(logLevel) | ||
if err != nil { | ||
// Do not crash on wrong user input, default to InfoLevel. | ||
level = zerolog.InfoLevel | ||
return nil, err | ||
} | ||
|
||
var l zerolog.Logger | ||
switch output { | ||
case "human": | ||
l = zerolog.New(humanConsoleWriter).Level(level).With().Timestamp().Logger() | ||
case "json": | ||
l = zerolog.New(os.Stdout).Level(level).With().Timestamp().Logger() | ||
default: | ||
l = zerolog.New(humanConsoleWriter).Level(level).With().Timestamp().Logger() | ||
} | ||
// use stdout, not stderr to maintain compatibility with zerolog | ||
loggerConfig.OutputPaths = []string{"stdout"} | ||
loggerConfig.ErrorOutputPaths = []string{"stdout"} | ||
|
||
return &l | ||
return loggerConfig.Build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,59 @@ | ||
package logger_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"github.com/smartcontractkit/timelock-worker/pkg/logger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogger(t *testing.T) { | ||
testLogger := logger.Logger("info", "human") | ||
assert.NotNil(t, testLogger) | ||
|
||
testType := reflect.TypeOf(testLogger) | ||
loggerType := reflect.TypeOf((*zerolog.Logger)(nil)) | ||
|
||
if !(loggerType == testType) { | ||
t.Errorf("testType does not implement loggerType") | ||
func Test_NewLogger(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
level string | ||
output string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "success: info human", | ||
level: "info", | ||
output: "human", | ||
}, | ||
{ | ||
name: "success: debug json", | ||
level: "info", | ||
output: "json", | ||
}, | ||
{ | ||
name: "invalid log level", | ||
level: "invalid", | ||
output: "human", | ||
wantErr: "unrecognized level: \"invalid\"", | ||
}, | ||
{ | ||
name: "invalid output", | ||
level: "info", | ||
output: "invalid", | ||
wantErr: "invalid logger output: \"invalid\"", | ||
}, | ||
} | ||
} | ||
|
||
func TestLoggerJSON(t *testing.T) { | ||
testLogger := logger.Logger("debug", "json") | ||
assert.NotNil(t, testLogger) | ||
|
||
testType := reflect.TypeOf(testLogger) | ||
loggerType := reflect.TypeOf((*zerolog.Logger)(nil)) | ||
|
||
if !(loggerType == testType) { | ||
t.Errorf("testType does not implement loggerType") | ||
} | ||
} | ||
|
||
func TestLoggerWrongLogLevel(t *testing.T) { | ||
testLogger := logger.Logger("wrongLogLevel", "human") | ||
assert.NotNil(t, testLogger) | ||
|
||
testType := reflect.TypeOf(testLogger) | ||
loggerType := reflect.TypeOf((*zerolog.Logger)(nil)) | ||
|
||
if !(loggerType == testType) { | ||
t.Errorf("testType does not implement loggerType") | ||
} | ||
} | ||
|
||
func TestLoggerWrongAll(t *testing.T) { | ||
testLogger := logger.Logger("wrongLogLevel", "wrongOutput") | ||
assert.NotNil(t, testLogger) | ||
|
||
testType := reflect.TypeOf(testLogger) | ||
loggerType := reflect.TypeOf((*zerolog.Logger)(nil)) | ||
|
||
if !(loggerType == testType) { | ||
t.Errorf("testType does not implement loggerType") | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
logger, err := logger.NewLogger(tt.level, tt.output) | ||
|
||
if tt.wantErr == "" { | ||
require.NoError(t, err) | ||
require.IsType(t, logger, new(zap.Logger)) | ||
} else { | ||
require.ErrorContains(t, err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.