|
|
|
@ -4,7 +4,6 @@ import (
|
|
|
|
|
"bytes" |
|
|
|
|
"encoding/json" |
|
|
|
|
"errors" |
|
|
|
|
"io" |
|
|
|
|
"os" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
@ -15,9 +14,7 @@ import (
|
|
|
|
|
func TestLogger_SetupBasic(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
require := require.New(t) |
|
|
|
|
cfg := &Config{ |
|
|
|
|
LogLevel: "INFO", |
|
|
|
|
} |
|
|
|
|
cfg := Config{LogLevel: "INFO"} |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, nil) |
|
|
|
|
require.NoError(err) |
|
|
|
@ -26,7 +23,7 @@ func TestLogger_SetupBasic(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func TestLogger_SetupInvalidLogLevel(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
cfg := &Config{} |
|
|
|
|
cfg := Config{} |
|
|
|
|
|
|
|
|
|
_, err := Setup(cfg, nil) |
|
|
|
|
testutil.RequireErrorContains(t, err, "Invalid log level") |
|
|
|
@ -61,7 +58,7 @@ func TestLogger_SetupLoggerErrorLevel(t *testing.T) {
|
|
|
|
|
require := require.New(t) |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(&cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.NoError(err) |
|
|
|
|
require.NotNil(logger) |
|
|
|
|
|
|
|
|
@ -79,12 +76,10 @@ func TestLogger_SetupLoggerErrorLevel(t *testing.T) {
|
|
|
|
|
func TestLogger_SetupLoggerDebugLevel(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
require := require.New(t) |
|
|
|
|
cfg := &Config{ |
|
|
|
|
LogLevel: "DEBUG", |
|
|
|
|
} |
|
|
|
|
cfg := Config{LogLevel: "DEBUG"} |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.NoError(err) |
|
|
|
|
require.NotNil(logger) |
|
|
|
|
|
|
|
|
@ -100,13 +95,13 @@ func TestLogger_SetupLoggerDebugLevel(t *testing.T) {
|
|
|
|
|
func TestLogger_SetupLoggerWithName(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
require := require.New(t) |
|
|
|
|
cfg := &Config{ |
|
|
|
|
cfg := Config{ |
|
|
|
|
LogLevel: "DEBUG", |
|
|
|
|
Name: "test-system", |
|
|
|
|
} |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.NoError(err) |
|
|
|
|
require.NotNil(logger) |
|
|
|
|
|
|
|
|
@ -118,14 +113,14 @@ func TestLogger_SetupLoggerWithName(t *testing.T) {
|
|
|
|
|
func TestLogger_SetupLoggerWithJSON(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
require := require.New(t) |
|
|
|
|
cfg := &Config{ |
|
|
|
|
cfg := Config{ |
|
|
|
|
LogLevel: "DEBUG", |
|
|
|
|
LogJSON: true, |
|
|
|
|
Name: "test-system", |
|
|
|
|
} |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.NoError(err) |
|
|
|
|
require.NotNil(logger) |
|
|
|
|
|
|
|
|
@ -146,13 +141,13 @@ func TestLogger_SetupLoggerWithValidLogPath(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
tmpDir := testutil.TempDir(t, t.Name()) |
|
|
|
|
|
|
|
|
|
cfg := &Config{ |
|
|
|
|
cfg := Config{ |
|
|
|
|
LogLevel: "INFO", |
|
|
|
|
LogFilePath: tmpDir + "/", |
|
|
|
|
} |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.NoError(err) |
|
|
|
|
require.NotNil(logger) |
|
|
|
|
} |
|
|
|
@ -161,13 +156,13 @@ func TestLogger_SetupLoggerWithInValidLogPath(t *testing.T) {
|
|
|
|
|
t.Parallel() |
|
|
|
|
require := require.New(t) |
|
|
|
|
|
|
|
|
|
cfg := &Config{ |
|
|
|
|
cfg := Config{ |
|
|
|
|
LogLevel: "INFO", |
|
|
|
|
LogFilePath: "nonexistentdir/", |
|
|
|
|
} |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.Error(err) |
|
|
|
|
require.True(errors.Is(err, os.ErrNotExist)) |
|
|
|
|
require.Nil(logger) |
|
|
|
@ -182,13 +177,13 @@ func TestLogger_SetupLoggerWithInValidLogPathPermission(t *testing.T) {
|
|
|
|
|
os.Mkdir(tmpDir, 0000) |
|
|
|
|
defer os.RemoveAll(tmpDir) |
|
|
|
|
|
|
|
|
|
cfg := &Config{ |
|
|
|
|
cfg := Config{ |
|
|
|
|
LogLevel: "INFO", |
|
|
|
|
LogFilePath: tmpDir + "/", |
|
|
|
|
} |
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
|
|
logger, err := Setup(cfg, []io.Writer{&buf}) |
|
|
|
|
logger, err := Setup(cfg, &buf) |
|
|
|
|
require.Error(err) |
|
|
|
|
require.True(errors.Is(err, os.ErrPermission)) |
|
|
|
|
require.Nil(logger) |
|
|
|
|