优化快捷方法&处理重复打印的BUG

This commit is contained in:
Yun
2024-04-21 12:23:26 +08:00
parent 864b1ae117
commit 328dadb308
4 changed files with 64 additions and 5 deletions
+37
View File
@@ -0,0 +1,37 @@
package loggerx
import "context"
var loggerc *Logger
func init() {
loggerc = NewLogger(context.Background())
}
func NewLoggerc(ctx context.Context, opts ...Option) {
for _, apply := range opts {
apply(&loggerc.option)
}
}
func Channel(ch string) (r *Logger) {
rr := *loggerc
rr.channel = ch
return &rr
}
func Info(ctx context.Context, v ...any) {
loggerc.Info(ctx, v...)
}
func Infof(ctx context.Context, format string, v ...any) {
loggerc.Infof(ctx, format, v...)
}
func Error(ctx context.Context, v ...any) {
loggerc.Error(ctx, v...)
}
func Errorf(ctx context.Context, format string, v ...any) {
loggerc.Errorf(ctx, format, v...)
}
+12
View File
@@ -0,0 +1,12 @@
package loggerx_test
import (
"context"
"testing"
"github.com/yuninks/loggerx"
)
func TestLoggerc(t *testing.T) {
loggerx.Info(context.Background(), "hhhhhh")
}
+5 -1
View File
@@ -95,7 +95,11 @@ func SetTimeZone() Option {
// 文件额外的驱动 // 文件额外的驱动
func SetExtraDriver(ds ...io.Writer) Option { func SetExtraDriver(ds ...io.Writer) Option {
return func(o *loggerOption) { return func(o *loggerOption) {
o.drivers = ds for _, d := range ds {
if d != nil {
o.drivers = append(o.drivers, d)
}
}
} }
} }
+10 -4
View File
@@ -15,9 +15,15 @@ func (l *Logger) write(event string, b []byte) (n int, err error) {
err = io.ErrShortWrite err = io.ErrShortWrite
} }
if err != nil { if err != nil {
// 强制更新 // 强制更新 & 再次写入
l.getFile(event, true) f, err := l.getFile(event, true)
if err == nil {
f.Write(b)
}
} }
d := append(l.option.drivers, f)
return io.MultiWriter(d...).Write(b) if len(l.option.drivers) > 0 {
io.MultiWriter(l.option.drivers...).Write(b)
}
return n, err
} }