Skip to content

Commit

Permalink
Change name of NewDefaultEmitter and NewStreamEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
huykingsofm committed Jan 6, 2023
1 parent 2704675 commit f22d3bc
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 33 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.4.1 (Jan 6, 2023)

- Change name of NewStreamEmitter to NewBufferEmitter.
- Change name of NewDefaultEmitter to NewStreamEmitter.
- Remove default logger of xybor.

# v0.4.0 (Jan 6, 2023)

- Allow determine the buffer size in emitter.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var handler = xylog.GetHandler("handler")
`StreamEmitter` is supported. You can use any `Writer` in this `Emitter` type.

```golang
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
```

When a logging method is called, the `Logger` creates a `LogRecord` and sends it
Expand Down Expand Up @@ -293,7 +293,7 @@ in different application zones.
```golang
// common/setup.go
func init() {
var emitter = xylog.NewDefaultEmitter(os.Stderr)
var emitter = xylog.NewStreamEmitter(os.Stderr)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)
handler.SetEncoding(encoding.NewJSONEncoding())
Expand Down
13 changes: 1 addition & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ import (
func init() {
rootLogger = newLogger("", nil)
rootLogger.SetLevel(WARNING)

var handler = GetHandler("xybor")
handler.AddEmitter(NewDefaultEmitter(os.Stderr))
handler.SetEncoding(encoding.NewTextEncoding())
handler.AddMacro("time", "asctime")
handler.AddMacro("level", "levelname")
handler.AddMacro("module", "name")

var logger = GetLogger("xybor")
logger.SetLevel(WARNING)
logger.AddHandler(handler)
}

// Default levels, these can be replaced with any positive set of values having
Expand Down Expand Up @@ -230,7 +219,7 @@ func (cfg SimpleConfig) Apply() (*Logger, error) {
}
}

var emitter = NewDefaultEmitter(writer)
var emitter = NewStreamEmitter(writer)

var enc = cfg.Encoding
if enc == nil {
Expand Down
12 changes: 6 additions & 6 deletions emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ type StreamEmitter struct {
lock *xylock.Lock
}

// NewStreamEmitter creates a StreamEmitter which writes message to a LogWriter
// (os.Stderr by default).
func NewStreamEmitter(w io.Writer, bufsize int) *StreamEmitter {
// NewBufferEmitter creates a StreamEmitter which uses a Buffered Writer.
func NewBufferEmitter(w io.Writer, bufsize int) *StreamEmitter {
xycond.AssertNotNil(w)

if bufsize != 0 {
Expand All @@ -66,9 +65,10 @@ func NewStreamEmitter(w io.Writer, bufsize int) *StreamEmitter {
return e
}

// NewDefaultEmitter creates a StreamEmitter not using the buffer size.
func NewDefaultEmitter(w io.Writer) *StreamEmitter {
return NewStreamEmitter(w, 0)
// NewStreamEmitter creates a StreamEmitter which writes logging message to a
// stream.
func NewStreamEmitter(w io.Writer) *StreamEmitter {
return NewBufferEmitter(w, 0)
}

// Emit will be called after a record was decided to log.
Expand Down
2 changes: 1 addition & 1 deletion emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

func TestNewStreamEmitterWithNil(t *testing.T) {
xycond.ExpectPanic(xyerror.AssertionError, func() {
xylog.NewDefaultEmitter(nil)
xylog.NewStreamEmitter(nil)
}).Test(t)
}

Expand Down
10 changes: 5 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ExampleSimpleConfig() {
}

func ExampleLogger() {
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)

Expand Down Expand Up @@ -93,7 +93,7 @@ func ExampleHandler() {
}

func ExampleEventLogger() {
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)

Expand All @@ -109,7 +109,7 @@ func ExampleEventLogger() {
}

func ExampleNewJSONEncoding() {
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)
handler.SetEncoding(encoding.NewJSONEncoding())
Expand All @@ -127,7 +127,7 @@ func ExampleNewJSONEncoding() {
}

func ExampleNewTextEncoding() {
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)
handler.SetEncoding(encoding.NewTextEncoding())
Expand All @@ -145,7 +145,7 @@ func ExampleNewTextEncoding() {
}

func ExampleFilter() {
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)
handler.AddFilter(&test.LoggerNameFilter{Name: "example.filter.chat"})
Expand Down
4 changes: 2 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestHandlerFileters(t *testing.T) {
}

func TestHandlerEmitters(t *testing.T) {
var emitter = xylog.NewDefaultEmitter(os.Stdout)
var emitter = xylog.NewStreamEmitter(os.Stdout)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)
xycond.ExpectEqual(len(handler.Emitters()), 1).Test(t)
Expand Down Expand Up @@ -109,6 +109,6 @@ func TestHandlerFullMacro(t *testing.T) {
xycond.ExpectEqual("asctime=ASCTIME created=1 filename=FILENAME "+
"funcname=FUNCNAME levelname=LEVELNAME levelno=2 lineno=3 "+
"module=MODULE msecs=4 pathname=PATHNAME process=5 "+
"relativeCreated=6", w.Captured).Test(t)
"relativeCreated=6\n", w.Captured).Test(t)
})
}
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestLoggerFindCaller(t *testing.T) {

func TestLoggerFlushParent(t *testing.T) {
var writer = &test.MockWriter{}
var emitter = xylog.NewDefaultEmitter(writer)
var emitter = xylog.NewStreamEmitter(writer)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)

Expand Down
8 changes: 4 additions & 4 deletions test/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// WithLogger allows using a Logger created with a MockWriter quickly.
func WithLogger(t *testing.T, f func(logger *xylog.Logger, w *MockWriter)) {
var writer = &MockWriter{}
var emitter = xylog.NewStreamEmitter(writer, 1)
var emitter = xylog.NewStreamEmitter(writer)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)

Expand All @@ -47,7 +47,7 @@ func WithLogger(t *testing.T, f func(logger *xylog.Logger, w *MockWriter)) {
// WithHandler allows using a Handler with MockWriter.
func WithHandler(t *testing.T, f func(h *xylog.Handler, w *MockWriter)) {
var writer = &MockWriter{}
var emitter = xylog.NewStreamEmitter(writer, 1)
var emitter = xylog.NewStreamEmitter(writer)
var handler = xylog.GetHandler(t.Name())
handler.AddEmitter(emitter)

Expand All @@ -58,13 +58,13 @@ func WithHandler(t *testing.T, f func(h *xylog.Handler, w *MockWriter)) {
// quickly.
func WithStreamEmitter(t *testing.T, f func(e *xylog.StreamEmitter, w *MockWriter)) {
var writer = &MockWriter{}
var emitter = xylog.NewStreamEmitter(writer, 1)
var emitter = xylog.NewStreamEmitter(writer)
f(emitter, writer)
}

// WithBenchLogger allows using a Logger whose output is io.Discard.
func WithBenchLogger(b *testing.B, f func(logger *xylog.Logger)) {
var emitter = xylog.NewStreamEmitter(io.Discard, 4096)
var emitter = xylog.NewBufferEmitter(io.Discard, 4096)
var handler = xylog.GetHandler("")
handler.AddEmitter(emitter)
handler.SetEncoding(encoding.NewJSONEncoding())
Expand Down

0 comments on commit f22d3bc

Please sign in to comment.