-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the WithTimeFormat function for Logger interface to set the time …
…format and return logger self (#16)
- Loading branch information
Showing
22 changed files
with
287 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/no-src/log/formatter" | ||
"github.com/no-src/log/level" | ||
) | ||
|
||
func TestConsoleLogger(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
formatter string | ||
concurrency bool | ||
timeFormat string | ||
}{ | ||
{"TextFormatter", formatter.TextFormatter, false, testTimeFormat}, | ||
{"JsonFormatter", formatter.JsonFormatter, false, ""}, | ||
{"TextFormatter Concurrency", formatter.TextFormatter, true, testTimeFormat}, | ||
{"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
InitDefaultLogger(NewConsoleLogger(level.DebugLevel).WithFormatter(formatter.New(tc.formatter)).WithTimeFormat(tc.timeFormat)) | ||
defer Close() | ||
if tc.concurrency { | ||
testLogsConcurrency(t, "TestConsoleLogger") | ||
} else { | ||
testLogs(t) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestConsoleLoggerWithBuffer(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
formatter string | ||
}{ | ||
{"TextFormatter", formatter.TextFormatter}, | ||
{"JsonFormatter", formatter.JsonFormatter}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
InitDefaultLogger(newConsoleLoggerWithBuffer(level.DebugLevel, true).WithFormatter(formatter.New(tc.formatter)).WithTimeFormat(testTimeFormat)) | ||
defer Close() | ||
testLogs(t) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package content | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/no-src/log/level" | ||
) | ||
|
||
func TestNewContent(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
err error | ||
appendTime bool | ||
timeFormat string | ||
}{ | ||
{"with nil error and no append time", nil, false, time.RFC3339}, | ||
{"with error and no append time", io.EOF, false, time.RFC3339}, | ||
{"with nil error and append time", nil, true, time.RFC3339}, | ||
{"with error and append time", io.EOF, true, time.RFC3339}, | ||
{"with empty time format", io.EOF, true, ""}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
c := NewContent(level.DebugLevel, tc.err, tc.appendTime, tc.timeFormat, tc.name) | ||
if c.AppendTime != tc.appendTime { | ||
t.Errorf("test NewContent failed, expect to get %v, but actual get %v", tc.appendTime, c.AppendTime) | ||
return | ||
} | ||
if tc.appendTime && c.Time == nil { | ||
t.Errorf("test NewContent failed, time can't be nil") | ||
return | ||
} | ||
|
||
if !tc.appendTime && c.Time != nil { | ||
t.Errorf("test NewContent failed, time should be nil") | ||
return | ||
} | ||
|
||
if tc.err != nil && c.Error == nil { | ||
t.Errorf("test NewContent failed, error can't be nil") | ||
} | ||
|
||
if tc.err == nil && c.Error != nil { | ||
t.Errorf("test NewContent failed, error should be nil") | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package content | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestError_MarshalText(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
err error | ||
expect string | ||
}{ | ||
{"with error", io.EOF, "EOF"}, | ||
{"with nil error", nil, ""}, | ||
} | ||
for _, tc := range testCases { | ||
cErr := NewError(tc.err).(Error) | ||
data, err := cErr.MarshalText() | ||
if err != nil { | ||
t.Errorf("Error.MarshalText error => %v", err) | ||
return | ||
} | ||
actual := string(data) | ||
if tc.expect != actual { | ||
t.Errorf("test Error.MarshalText failed, expect to get %s, but actual get %s", tc.expect, actual) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package content | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestTime_MarshalText(t *testing.T) { | ||
now := time.Now() | ||
expect := now.Format(DefaultLogTimeFormat) | ||
ti := NewTime(time.Now()) | ||
data, err := ti.MarshalText() | ||
if err != nil { | ||
t.Errorf("Time.MarshalText error => %v", err) | ||
return | ||
} | ||
actual := string(data) | ||
if expect != actual { | ||
t.Errorf("test Time.MarshalText failed, expect to get %s, but actual get %s", expect, actual) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/no-src/log/formatter" | ||
) | ||
|
||
func TestEmptyLogger(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
formatter string | ||
}{ | ||
{"TextFormatter", formatter.TextFormatter}, | ||
{"JsonFormatter", formatter.JsonFormatter}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
InitDefaultLogger(NewEmptyLogger().WithFormatter(formatter.New(tc.formatter)).WithTimeFormat(testTimeFormat)) | ||
defer Close() | ||
testLogs(t) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.