Skip to content

Commit

Permalink
调整代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 13, 2023
1 parent df71d72 commit dd83e25
Show file tree
Hide file tree
Showing 35 changed files with 156 additions and 118 deletions.
3 changes: 2 additions & 1 deletion FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* [x] 完善示例代码
* [x] 增加属性解析器适配功能
* [x] 优化 handler 和 writer 设计
* [ ] 增加一个可读性高的 handler 实现
* [x] 增加一个可读性高的 handler 实现
* [ ] MixHandler 转义处理
* [ ] 提高单元测试覆盖率到 70%
* [ ] 进一步提高单元测试覆盖率到 80%

Expand Down
2 changes: 1 addition & 1 deletion _examples/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"github.com/FishGoddess/logit"
"github.com/FishGoddess/logit/rotate"
"github.com/FishGoddess/logit/core/rotate"
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions _examples/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"log/slog"

"github.com/FishGoddess/logit"
"github.com/FishGoddess/logit/core/handler"
)

func main() {
Expand All @@ -31,12 +32,12 @@ func main() {
logger = logit.NewLogger(logit.WithJsonHandler())
logger.Info("using json handler")

// Or you want to use customized handlers, try RegisterHandler.
// Or you want to use customized handlers, try Register.
newHandler := func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return slog.NewTextHandler(w, opts)
}

if err := logit.RegisterHandler("demo", newHandler); err != nil {
if err := handler.Register("demo", newHandler); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion _examples/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {

// Change logger handler:
logit.WithHandler("xxx")
logit.WithStandardHandler()
logit.WithMixHandler()
logit.WithTextHandler()
logit.WithJsonHandler()

Expand Down
10 changes: 5 additions & 5 deletions _examples/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ BenchmarkLogrusFile-2 174944 6491 ns/op
func BenchmarkLogitLogger(b *testing.B) {
logger := logit.NewLogger(
logit.WithInfoLevel(),
logit.WithStandardHandler(),
logit.WithMixHandler(),
logit.WithWriter(io.Discard),
)

Expand Down Expand Up @@ -107,7 +107,7 @@ func BenchmarkLogitLoggerJsonHandler(b *testing.B) {
func BenchmarkLogitLoggerPrint(b *testing.B) {
logger := logit.NewLogger(
logit.WithInfoLevel(),
logit.WithStandardHandler(),
logit.WithMixHandler(),
logit.WithWriter(io.Discard),
)

Expand Down Expand Up @@ -226,7 +226,7 @@ func BenchmarkLogitFile(b *testing.B) {

logger := logit.NewLogger(
logit.WithInfoLevel(),
logit.WithStandardHandler(),
logit.WithMixHandler(),
logit.WithFile(path),
)

Expand All @@ -244,7 +244,7 @@ func BenchmarkLogitFileWithBuffer(b *testing.B) {

logger := logit.NewLogger(
logit.WithInfoLevel(),
logit.WithStandardHandler(),
logit.WithMixHandler(),
logit.WithFile(path),
logit.WithBuffer(65536),
)
Expand All @@ -265,7 +265,7 @@ func BenchmarkLogitFileWithBatch(b *testing.B) {

logger := logit.NewLogger(
logit.WithInfoLevel(),
logit.WithStandardHandler(),
logit.WithMixHandler(),
logit.WithFile(path),
logit.WithBatch(64),
)
Expand Down
6 changes: 4 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"log/slog"
"os"
"time"

"github.com/FishGoddess/logit/core/handler"
)

type nilSyncer struct{}
Expand Down Expand Up @@ -55,7 +57,7 @@ func newDefaultConfig() *config {

conf := &config{
level: slog.LevelDebug,
handler: handlerStandard,
handler: handler.Mix,
newWriter: newWriter,
wrapWriter: nil,
replaceAttr: nil,
Expand Down Expand Up @@ -102,7 +104,7 @@ func (c *config) newHandlerOptions() *slog.HandlerOptions {
}

func (c *config) newHandler() (slog.Handler, Syncer, io.Closer, error) {
newHandler, err := getHandlerFunc(c.handler)
newHandler, err := handler.Get(c.handler)
if err != nil {
return nil, nil, nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"log/slog"
"os"
"testing"

"github.com/FishGoddess/logit/core/handler"
)

type testConfigHandler struct {
Expand Down Expand Up @@ -58,7 +60,7 @@ func TestConfigNewHandlerOptions(t *testing.T) {
func TestConfigNewHandler(t *testing.T) {
handlerName := t.Name()

RegisterHandler(handlerName, func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
handler.Register(handlerName, func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return &testConfigHandler{
w: w,
opts: *opts,
Expand Down
2 changes: 1 addition & 1 deletion buffer.go → core/handler/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package logit
package handler

import (
"sync"
Expand Down
2 changes: 1 addition & 1 deletion buffer_test.go → core/handler/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package logit
package handler

import (
"testing"
Expand Down
32 changes: 16 additions & 16 deletions handler.go → core/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package logit
package handler

import (
"fmt"
Expand All @@ -22,20 +22,20 @@ import (
)

const (
handlerStandard = "standard"
handlerText = "text"
handlerJson = "json"
Mix = "mix"
Text = "text"
Json = "json"
)

var (
newHandlers = map[string]HandlerFunc{
handlerStandard: func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return NewStandardHandler(w, opts)
newHandlers = map[string]NewHandlerFunc{
Mix: func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return NewMixHandler(w, opts)
},
handlerText: func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
Text: func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return slog.NewTextHandler(w, opts)
},
handlerJson: func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
Json: func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return slog.NewJSONHandler(w, opts)
},
}
Expand All @@ -45,23 +45,23 @@ var (
newHandlersLock sync.RWMutex
)

// HandlerFunc is a function for creating slog.Handler with w and opts.
type HandlerFunc func(w io.Writer, opts *slog.HandlerOptions) slog.Handler
// NewHandlerFunc is a function for creating slog.Handler with w and opts.
type NewHandlerFunc func(w io.Writer, opts *slog.HandlerOptions) slog.Handler

// getHandlerFunc gets new handler func with name and returns an error if failed.
func getHandlerFunc(name string) (HandlerFunc, error) {
// Get gets new handler func with name and returns an error if failed.
func Get(name string) (NewHandlerFunc, error) {
newHandlersLock.RLock()
defer newHandlersLock.RUnlock()

if newHandler, ok := newHandlers[name]; ok {
return newHandler, nil
}

return nil, fmt.Errorf("logit: handler %s unknown", name)
return nil, fmt.Errorf("logit: handler %s not found", name)
}

// RegisterHandler registers newHandler with name to logit.
func RegisterHandler(name string, newHandler HandlerFunc) error {
// Register registers newHandler with name.
func Register(name string, newHandler NewHandlerFunc) error {
newHandlersLock.Lock()
defer newHandlersLock.Unlock()

Expand Down
12 changes: 6 additions & 6 deletions handler_test.go → core/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package logit
package handler

import (
"fmt"
Expand All @@ -30,7 +30,7 @@ func TestGetHandlerFunc(t *testing.T) {
handler := t.Name()
newHandlers[handler] = newHandler

got, err := getHandlerFunc(handler)
got, err := Get(handler)
if err != nil {
t.Fatal(err)
}
Expand All @@ -40,18 +40,18 @@ func TestGetHandlerFunc(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestRegisterHandler$
func TestRegisterHandler(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestRegister$
func TestRegister(t *testing.T) {
for name := range newHandlers {
if err := RegisterHandler(name, nil); err == nil {
if err := Register(name, nil); err == nil {
t.Fatal("register an existed handler func should be failed")
}
}

handler := t.Name()
newHandler := func(w io.Writer, opts *slog.HandlerOptions) slog.Handler { return nil }

if err := RegisterHandler(handler, newHandler); err != nil {
if err := Register(handler, newHandler); err != nil {
t.Fatal(err)
}

Expand Down
Loading

0 comments on commit dd83e25

Please sign in to comment.