Add libbox wrapper

This commit is contained in:
世界
2022-10-25 12:55:00 +08:00
parent 86e55c5c1c
commit 222196b182
30 changed files with 829 additions and 100 deletions

View File

@@ -12,16 +12,22 @@ import (
var _ Factory = (*simpleFactory)(nil)
type simpleFactory struct {
formatter Formatter
writer io.Writer
level Level
formatter Formatter
platformFormatter Formatter
writer io.Writer
platformWriter io.Writer
level Level
}
func NewFactory(formatter Formatter, writer io.Writer) Factory {
func NewFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) Factory {
return &simpleFactory{
formatter: formatter,
writer: writer,
level: LevelTrace,
platformFormatter: Formatter{
BaseTime: formatter.BaseTime,
},
writer: writer,
platformWriter: platformWriter,
level: LevelTrace,
}
}
@@ -53,7 +59,8 @@ func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) {
if level > l.level {
return
}
message := l.formatter.Format(ctx, level, l.tag, F.ToString(args...), time.Now())
nowTime := time.Now()
message := l.formatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)
if level == LevelPanic {
panic(message)
}
@@ -61,6 +68,9 @@ func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) {
if level == LevelFatal {
os.Exit(1)
}
if l.platformWriter != nil {
l.platformWriter.Write([]byte(l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
}
}
func (l *simpleLogger) Trace(args ...any) {

View File

@@ -9,7 +9,7 @@ import (
var std ContextLogger
func init() {
std = NewFactory(Formatter{BaseTime: time.Now()}, os.Stderr).Logger()
std = NewFactory(Formatter{BaseTime: time.Now()}, os.Stderr, nil).Logger()
}
func StdLogger() ContextLogger {

View File

@@ -14,19 +14,25 @@ import (
var _ Factory = (*observableFactory)(nil)
type observableFactory struct {
formatter Formatter
writer io.Writer
level Level
subscriber *observable.Subscriber[Entry]
observer *observable.Observer[Entry]
formatter Formatter
platformFormatter Formatter
writer io.Writer
platformWriter io.Writer
level Level
subscriber *observable.Subscriber[Entry]
observer *observable.Observer[Entry]
}
func NewObservableFactory(formatter Formatter, writer io.Writer) ObservableFactory {
func NewObservableFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) ObservableFactory {
factory := &observableFactory{
formatter: formatter,
writer: writer,
level: LevelTrace,
subscriber: observable.NewSubscriber[Entry](128),
formatter: formatter,
platformFormatter: Formatter{
BaseTime: formatter.BaseTime,
},
writer: writer,
platformWriter: platformWriter,
level: LevelTrace,
subscriber: observable.NewSubscriber[Entry](128),
}
factory.observer = observable.NewObserver[Entry](factory.subscriber, 64)
return factory
@@ -74,7 +80,8 @@ func (l *observableLogger) Log(ctx context.Context, level Level, args []any) {
if level > l.level {
return
}
message, messageSimple := l.formatter.FormatWithSimple(ctx, level, l.tag, F.ToString(args...), time.Now())
nowTime := time.Now()
message, messageSimple := l.formatter.FormatWithSimple(ctx, level, l.tag, F.ToString(args...), nowTime)
if level == LevelPanic {
panic(message)
}
@@ -83,6 +90,9 @@ func (l *observableLogger) Log(ctx context.Context, level Level, args []any) {
os.Exit(1)
}
l.subscriber.Emit(Entry{level, messageSimple})
if l.platformWriter != nil {
l.platformWriter.Write([]byte(l.formatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
}
}
func (l *observableLogger) Trace(args ...any) {