Skip to content

Commit

Permalink
fix: add sync writer (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Oct 21, 2023
2 parents 4c82572 + 4d82f86 commit ea0a790
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
6 changes: 3 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestContext(t *testing.T) {
//nolint:paralleltest,tparallel
t.Run("success", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
ctx := WithContext(context.Background(), NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())
ctx := WithContext(context.Background(), NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build())
l := FromContext(ctx)
l.Debugf("Debugf")
if expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Debugf"}`); !expected.Match(buf.Bytes()) {
Expand All @@ -24,7 +24,7 @@ func TestContext(t *testing.T) {

t.Run("failure,nilContext", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer SetGlobal(NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())()
defer SetGlobal(NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build())()
_ = FromContext(nil) //nolint:staticcheck
if expected := regexp.MustCompilePOSIX(`{"severity":"ERROR","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+\.go:[0-9]+","message":"ilog: nil context"}`); !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand All @@ -34,7 +34,7 @@ func TestContext(t *testing.T) {

t.Run("failure,invalidType", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer SetGlobal(NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())()
defer SetGlobal(NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build())()
_ = FromContext(context.WithValue(context.Background(), contextKeyLogger{}, "invalid")) //nolint:staticcheck
if expected := regexp.MustCompilePOSIX(`{"severity":"ERROR","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+\.go:[0-9]+","message":"ilog: type assertion failed: expected=ilog.Logger, actual=string, value=\\"invalid\\""}`); !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand Down
4 changes: 2 additions & 2 deletions global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGlobal(t *testing.T) {
//nolint:paralleltest,tparallel
t.Run("success", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer SetGlobal(NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())()
defer SetGlobal(NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build())()
expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Logf","any":"any","bool":true,"bytes":"bytes","duration":"1h1m1.001001001s","error":"unexpected EOF","eof":"EOF","float32":1\.234567,"float64":1\.23456789,"int":-1,"int32":123456789,"int64":123456789,"string":"string","time":"2023-08-13T04:38:39\.123456789\+09:00","uint":1,"uint32":123456789,"uint64":123456789}`)
l := Global().Copy().
Any("any", "any").
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestSetStdLogger(t *testing.T) {
t.Run("success", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog.go/global_test.go:[0-9]+","message":"Print\\n","any":"any","bool":true,"bytes":"bytes","duration":"1h1m1.001001001s","error":"unexpected EOF","eof":"EOF","float32":1\.234567,"float64":1\.23456789,"int":-1,"int32":123456789,"int64":123456789,"string":"string","time":"2023-08-13T04:38:39\.123456789\+09:00","uint":1,"uint32":123456789,"uint64":123456789}` + "\n")
l := NewBuilder(DebugLevel, buf).
l := NewBuilder(DebugLevel, NewSyncWriter(buf)).
SetTimestampZone(time.UTC).
Build().
Any("any", "any").
Expand Down
15 changes: 15 additions & 0 deletions ilog_default_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ type implLogger struct {
fields []byte
}

type syncWriter struct {
mu sync.Mutex
w io.Writer
}

func (w *syncWriter) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.w.Write(p) //nolint:wrapcheck
}

func NewSyncWriter(w io.Writer) io.Writer {
return &syncWriter{w: w}
}

// NewBuilder returns a new Builder of ilog.Logger with the specified level and writer.
func NewBuilder(level Level, w io.Writer) implLoggerConfig { //nolint:revive
return implLoggerConfig{
Expand Down
30 changes: 15 additions & 15 deletions ilog_default_implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestScenario(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Logf: format string","bool":true,"boolPointer":false,"boolPointer2":null,"byte":"\\u0001","bytes":"bytes","time\.Duration":"1h1m1.001001001s","error":"ilog: log entry not written","errorFormatter":"ilog: log entry not written","errorNil":null,"float32":1\.234567,"float64":1\.23456789,"float64NaN":"NaN","float64\+Inf":"\+Inf","float64-Inf":"-Inf","int":-1,"int8":-1,"int16":-1,"int32":123456789,"int64":123456789,"string":"string","stringEscaped":"\\b\\f\\n\\r\\t","time\.Time":"2023-08-13T04:38:39\.123456789\+09:00","uint":1,"uint16":1,"uint32":123456789,"uint64":123456789,"jsonSuccess":{"json":true},"jsonFailure":"json.Marshaler: v.MarshalJSON: unexpected EOF","jsonNull":null,"fmt\.Formatter":"testFormatter","fmt\.Stringer":"testStringer","fmt.StringerNull":null,"func":"0x[0-9a-f]+","mapSuccess":{"map":{"in":1}},"mapFailure":"map\[map:0x[0-9a-f]+\]","sliceSuccess":\["a","b"\],"sliceFailure":"\[0x[0-9a-f]+\]","append":"logger"}`)

l := NewBuilder(DebugLevel, buf).
l := NewBuilder(DebugLevel, NewSyncWriter(buf)).
SetTimestampZone(time.UTC).
Build().
Any("bool", true).
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestLogger(t *testing.T) {
expected := regexp.MustCompilePOSIX(`{"level":"DEBUG","time":"[0-9]+-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}","file":".+/ilog\.go/[a-z_]+_test\.go:[0-9]+","msg":"Logf"}` + "\r\n")

const expectedLevel = DebugLevel
l := NewBuilder(ErrorLevel, buf).
l := NewBuilder(ErrorLevel, NewSyncWriter(buf)).
SetLevelKey("level").
SetLevels(copyLevels(defaultLevels)).
SetTimestampKey("time").
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestLogger(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer t.Logf("ℹ️: buf:\n%s", buf)

l := NewBuilder(DebugLevel, buf).SetTimestampKey("").SetCallerKey("").Build()
l := NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampKey("").SetCallerKey("").Build()
l.Any("any", "any").Debugf("Debugf")
l.Bool("bool", true).Debugf("Debugf")
l.Bytes("bytes", []byte("bytes")).Debugf("Debugf")
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestLogEntry(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Logf: format string","bool":true,"boolPointer":false,"boolPointer2":null,"byte":"\\u0001","bytes":"bytes","time\.Duration":"1h1m1.001001001s","error":"ilog: log entry not written","errorFormatter":"ilog: log entry not written","errorNil":null,"float32":1\.234567,"float64":1\.23456789,"float64NaN":"NaN","float64\+Inf":"\+Inf","float64-Inf":"-Inf","int":-1,"int8":-1,"int16":-1,"int32":123456789,"int64":123456789,"string":"string","stringEscaped":"\\b\\f\\n\\r\\t","time\.Time":"2023-08-13T04:38:39\.123456789\+09:00","uint":1,"uint16":1,"uint32":123456789,"uint64":123456789,"jsonSuccess":{"json":true},"jsonFailure":"json.Marshaler: v.MarshalJSON: unexpected EOF","jsonNull":null,"fmt\.Formatter":"testFormatter","fmt\.Stringer":"testStringer","fmt.StringerNull":null,"func":"0x[0-9a-f]+","mapSuccess":{"map":{"in":1}},"mapFailure":"map\[map:0x[0-9a-f]+\]","sliceSuccess":\["a","b"\],"sliceFailure":"\[0x[0-9a-f]+\]"}`)

le := NewBuilder(DebugLevel, buf).
le := NewBuilder(DebugLevel, NewSyncWriter(buf)).
SetTimestampZone(time.UTC).
Build().
Any("bool", true).
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestLogEntry(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"default"}`)

NewBuilder(-128, buf).SetTimestampZone(time.UTC).Build().Logf(-128, "default")
NewBuilder(-128, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Logf(-128, "default")

if !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand All @@ -306,7 +306,7 @@ func TestLogEntry(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Debugf"}`)

NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build().Debugf("Debugf")
NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Debugf("Debugf")

if !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand All @@ -320,7 +320,7 @@ func TestLogEntry(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"INFO","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Infof"}`)

NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build().Infof("Infof")
NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Infof("Infof")

if !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand All @@ -334,7 +334,7 @@ func TestLogEntry(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"WARN","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Warnf"}`)

NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build().Warnf("Warnf")
NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Warnf("Warnf")

if !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand All @@ -348,7 +348,7 @@ func TestLogEntry(t *testing.T) {

expected := regexp.MustCompilePOSIX(`{"severity":"ERROR","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"Errorf"}`)

NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build().Errorf("Errorf")
NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Errorf("Errorf")

if !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
Expand All @@ -364,19 +364,19 @@ func TestLogger_logf(t *testing.T) {

const expected = ""

NewBuilder(InfoLevel, buf).SetTimestampZone(time.UTC).Build().Debugf("Debugf")
NewBuilder(InfoLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Debugf("Debugf")
if expected != buf.String() {
t.Errorf("❌: expected(%s) != actual(%s)", expected, buf.String())
}
buf.Reset()

NewBuilder(WarnLevel, buf).SetTimestampZone(time.UTC).Build().Infof("Infof")
NewBuilder(WarnLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Infof("Infof")
if expected != buf.String() {
t.Errorf("❌: expected(%s) != actual(%s)", expected, buf.String())
}
buf.Reset()

NewBuilder(ErrorLevel, buf).SetTimestampZone(time.UTC).Build().Warnf("Warnf")
NewBuilder(ErrorLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build().Warnf("Warnf")
if expected != buf.String() {
t.Errorf("❌: expected(%s) != actual(%s)", expected, buf.String())
}
Expand All @@ -390,7 +390,7 @@ func TestLogger_logf(t *testing.T) {

const expected = "{}\n"

NewBuilder(DebugLevel, buf).
NewBuilder(DebugLevel, NewSyncWriter(buf)).
SetLevelKey("").
SetTimestampKey("").
SetCallerKey("").
Expand All @@ -417,7 +417,7 @@ func TestLogger_Write(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer t.Logf("ℹ️: buf:\n%s", buf)

defer SetGlobal(NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())()
defer SetGlobal(NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build())()

i, err := NewBuilder(DebugLevel, &testWriter{err: io.ErrUnexpectedEOF}).SetTimestampZone(time.UTC).Build().Write([]byte("ERROR"))
if expected := regexp.MustCompilePOSIX(`w.logf: w.logger.writer.Write: p={"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"ERROR"}: unexpected EOF`); err == nil || !expected.MatchString(err.Error()) {
Expand All @@ -435,7 +435,7 @@ func TestLogger_Write(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer t.Logf("ℹ️: buf:\n%s", buf)

defer SetGlobal(NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())()
defer SetGlobal(NewBuilder(DebugLevel, NewSyncWriter(buf)).SetTimestampZone(time.UTC).Build())()

i, err := NewBuilder(DebugLevel, &testWriter{err: io.ErrUnexpectedEOF}).SetTimestampZone(time.UTC).Build().Any("any", "any").Write([]byte("ERROR"))
if expected := regexp.MustCompilePOSIX(`w.logf: w.logger.writer.Write: p={"severity":"DEBUG","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+_test\.go:[0-9]+","message":"ERROR","any":"any"}: unexpected EOF`); err == nil || !expected.MatchString(err.Error()) {
Expand Down

0 comments on commit ea0a790

Please sign in to comment.