mirror of https://github.com/sirupsen/logrus.git
This commit is contained in:
parent
85981c0459
commit
2840389b1a
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue