优化快捷方法&处理重复打印的BUG
This commit is contained in:
+37
@@ -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...)
|
||||
}
|
||||
@@ -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
@@ -95,7 +95,11 @@ func SetTimeZone() Option {
|
||||
// 文件额外的驱动
|
||||
func SetExtraDriver(ds ...io.Writer) Option {
|
||||
return func(o *loggerOption) {
|
||||
o.drivers = ds
|
||||
for _, d := range ds {
|
||||
if d != nil {
|
||||
o.drivers = append(o.drivers, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-4
@@ -15,9 +15,15 @@ func (l *Logger) write(event string, b []byte) (n int, err error) {
|
||||
err = io.ErrShortWrite
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user