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