Skip to content

Commit

Permalink
增加包级别的 Sync 和 Close 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 21, 2023
1 parent 92dd0ee commit ac9337a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v1.5.7-alpha

> 此版本发布于 2023-12-22
* 增加包级别的 Sync 和 Close 函数

### v1.5.6-alpha

> 此版本发布于 2023-12-21
Expand Down
10 changes: 10 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ func Println(args ...interface{}) {
msg := fmt.Sprintln(args...)
Default().log(defaults.LevelPrint, msg)
}

// Sync syncs the default logger and returns an error if failed.
func Sync() error {
return Default().Sync()
}

// Close closes the default logger and returns an error if failed.
func Close() error {
return Default().Close()
}
45 changes: 45 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,48 @@ func TestDefaultLogger(t *testing.T) {
t.Fatalf("got %s != want %s", got, want)
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefaultLoggerSync$
func TestDefaultLoggerSync(t *testing.T) {
syncer := &testSyncer{
synced: false,
}

logger := &Logger{
syncer: syncer,
}

SetDefault(logger)
Sync()

if !syncer.synced {
t.Fatal("syncer.synced is wrong")
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefaultLoggerClose$
func TestDefaultLoggerClose(t *testing.T) {
syncer := &testSyncer{
synced: false,
}

closer := &testCloser{
closed: false,
}

logger := &Logger{
syncer: syncer,
closer: closer,
}

SetDefault(logger)
Close()

if !syncer.synced {
t.Fatal("syncer.synced is wrong")
}

if !closer.closed {
t.Fatal("closer.closed is wrong")
}
}
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,5 @@ package logit // import "github.com/FishGoddess/logit"

const (
// Version is the version string representation of logit.
Version = "v1.5.6-alpha"
Version = "v1.5.7-alpha"
)
61 changes: 61 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ type testLoggerHandler struct {
opts slog.HandlerOptions
}

type testSyncer struct {
synced bool
}

func (ts *testSyncer) Sync() error {
ts.synced = true
return nil
}

type testCloser struct {
closed bool
}

func (tc *testCloser) Close() error {
tc.closed = true
return nil
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestNewLogger$
func TestNewLogger(t *testing.T) {
handlerName := t.Name()
Expand Down Expand Up @@ -219,3 +237,46 @@ func TestLogger(t *testing.T) {
t.Fatalf("got %s != want %s", got, want)
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestLoggerSync$
func TestLoggerSync(t *testing.T) {
syncer := &testSyncer{
synced: false,
}

logger := &Logger{
syncer: syncer,
}

logger.Sync()

if !syncer.synced {
t.Fatal("syncer.synced is wrong")
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestLoggerClose$
func TestLoggerClose(t *testing.T) {
syncer := &testSyncer{
synced: false,
}

closer := &testCloser{
closed: false,
}

logger := &Logger{
syncer: syncer,
closer: closer,
}

logger.Close()

if !syncer.synced {
t.Fatal("syncer.synced is wrong")
}

if !closer.closed {
t.Fatal("closer.closed is wrong")
}
}

0 comments on commit ac9337a

Please sign in to comment.