Skip to content

Commit

Permalink
fix: fix nil option
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Mar 6, 2024
1 parent 731b5d2 commit f88ece4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
10 changes: 6 additions & 4 deletions rotate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package logh

import (
"io"
"log/slog"
)

func NewRotateJSONHandler(rollSize int, flushInterval int, opts *slog.HandlerOptions) (slog.Handler, error) {
var w io.Writer
func NewRotateJSONHandler(directory string, basename string, rollSize int, opts *slog.HandlerOptions, options ...Option) (slog.Handler, error) {
file, err := NewRotateFile(directory, basename, rollSize, options...)
if err != nil {
return nil, err
}

return slog.NewJSONHandler(w, opts), nil
return slog.NewJSONHandler(file, opts), nil
}
4 changes: 3 additions & 1 deletion rotate_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func NewRotateFile(directory string, basename string, rotateSize int, opts ...Op
}

for _, opt := range opts {
opt(rf)
if opt != nil {
opt(rf)
}
}

rf.rotate()
Expand Down
2 changes: 1 addition & 1 deletion rotate_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNewRotateFile(t *testing.T) {
defer os.RemoveAll(path)

path += "/TestNewRotateFile"
file, err := NewRotateFile(path, "test", 10)
file, err := NewRotateFile(path, "test", 10, nil)
require.NoError(t, err)
require.NotNil(t, file)

Expand Down
17 changes: 13 additions & 4 deletions rotate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package logh

// func TestRotateJSONHandler(t *testing.T) {
// handler, err := NewRotateJSONHandler(0, 0, nil)
// require.NoError(t, err)
import (
"log/slog"
"testing"

// }
"github.com/stretchr/testify/require"
)

func TestRotateJSONHandler(t *testing.T) {
handler, err := NewRotateJSONHandler("test", "test", 10, nil, nil)
require.NoError(t, err)

logger := slog.New(handler)
logger.Info("test")
}

0 comments on commit f88ece4

Please sign in to comment.