This commit is contained in:
Vincent Tsao 2022-01-26 12:37:10 -05:00
parent 85981c0459
commit 2840389b1a
2 changed files with 29 additions and 1 deletions

View File

@ -387,6 +387,13 @@ func (logger *Logger) SetFormatter(formatter Formatter) {
logger.Formatter = formatter logger.Formatter = formatter
} }
// Output returns the IO writer for this logger in a concurrency safe manner.
func (logger *Logger) Output() io.Writer {
logger.mu.Lock()
defer logger.mu.Unlock()
return logger.Out
}
// SetOutput sets the logger output. // SetOutput sets the logger output.
func (logger *Logger) SetOutput(output io.Writer) { func (logger *Logger) SetOutput(output io.Writer) {
logger.mu.Lock() logger.mu.Lock()

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"sync"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -95,3 +96,23 @@ func TestLogger_SetBufferPool(t *testing.T) {
assert.Equal(t, pool.get, 1, "Logger.SetBufferPool(): The BufferPool.Get() must be called") assert.Equal(t, pool.get, 1, "Logger.SetBufferPool(): The BufferPool.Get() must be called")
assert.Len(t, pool.buffers, 1, "Logger.SetBufferPool(): The BufferPool.Put() must be called") assert.Len(t, pool.buffers, 1, "Logger.SetBufferPool(): The BufferPool.Put() must be called")
} }
func TestOutputRace(t *testing.T) {
l := New()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_ = l.Output()
}()
wg.Add(1)
go func() {
defer wg.Done()
out := &bytes.Buffer{}
l.SetOutput(out)
}()
wg.Wait()
}