mirror of https://github.com/sirupsen/logrus.git
Adds WithTime to Logger and Entry types, as well as a pure module-level function.
This commit is contained in:
parent
52b92f5b89
commit
725f3be199
7
entry.go
7
entry.go
|
@ -83,7 +83,12 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||||
for k, v := range fields {
|
for k, v := range fields {
|
||||||
data[k] = v
|
data[k] = v
|
||||||
}
|
}
|
||||||
return &Entry{Logger: entry.Logger, Data: data}
|
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overrides the time of the Entry.
|
||||||
|
func (entry *Entry) WithTime(t time.Time) *Entry {
|
||||||
|
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is not declared with a pointer value because otherwise
|
// This function is not declared with a pointer value because otherwise
|
||||||
|
|
10
exported.go
10
exported.go
|
@ -2,6 +2,7 @@ package logrus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -72,6 +73,15 @@ func WithFields(fields Fields) *Entry {
|
||||||
return std.WithFields(fields)
|
return std.WithFields(fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTime creats an entry from the standard logger and overrides the time of
|
||||||
|
// logs generated with it.
|
||||||
|
//
|
||||||
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
||||||
|
// or Panic on the Entry it returns.
|
||||||
|
func WithTime(t time.Time) *Entry {
|
||||||
|
return std.WithTime(t)
|
||||||
|
}
|
||||||
|
|
||||||
// Debug logs a message at level Debug on the standard logger.
|
// Debug logs a message at level Debug on the standard logger.
|
||||||
func Debug(args ...interface{}) {
|
func Debug(args ...interface{}) {
|
||||||
std.Debug(args...)
|
std.Debug(args...)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
|
@ -112,6 +113,13 @@ func (logger *Logger) WithError(err error) *Entry {
|
||||||
return entry.WithError(err)
|
return entry.WithError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Overrides the time of the log entry.
|
||||||
|
func (logger *Logger) WithTime(t time.Time) *Entry {
|
||||||
|
entry := logger.newEntry()
|
||||||
|
defer logger.releaseEntry(entry)
|
||||||
|
return entry.WithTime(t)
|
||||||
|
}
|
||||||
|
|
||||||
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
||||||
if logger.level() >= DebugLevel {
|
if logger.level() >= DebugLevel {
|
||||||
entry := logger.newEntry()
|
entry := logger.newEntry()
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -209,6 +210,65 @@ func TestDefaultFieldsAreNotPrefixed(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithTimeShouldOverrideTime(t *testing.T) {
|
||||||
|
now := time.Now().Add(24 * time.Hour)
|
||||||
|
|
||||||
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
|
log.WithTime(now).Info("foobar")
|
||||||
|
}, func(fields Fields) {
|
||||||
|
assert.Equal(t, fields["time"], now.Format(defaultTimestampFormat))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithTimeShouldNotOverrideFields(t *testing.T) {
|
||||||
|
now := time.Now().Add(24 * time.Hour)
|
||||||
|
|
||||||
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
|
log.WithField("herp", "derp").WithTime(now).Info("blah")
|
||||||
|
}, func(fields Fields) {
|
||||||
|
assert.Equal(t, fields["time"], now.Format(defaultTimestampFormat))
|
||||||
|
assert.Equal(t, fields["herp"], "derp")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithFieldShouldNotOverrideTime(t *testing.T) {
|
||||||
|
now := time.Now().Add(24 * time.Hour)
|
||||||
|
|
||||||
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
|
log.WithTime(now).WithField("herp", "derp").Info("blah")
|
||||||
|
}, func(fields Fields) {
|
||||||
|
assert.Equal(t, fields["time"], now.Format(defaultTimestampFormat))
|
||||||
|
assert.Equal(t, fields["herp"], "derp")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeOverrideMultipleLogs(t *testing.T) {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
var firstFields, secondFields Fields
|
||||||
|
|
||||||
|
logger := New()
|
||||||
|
logger.Out = &buffer
|
||||||
|
formatter := new(JSONFormatter)
|
||||||
|
formatter.TimestampFormat = time.StampMilli
|
||||||
|
logger.Formatter = formatter
|
||||||
|
|
||||||
|
llog := logger.WithField("herp", "derp")
|
||||||
|
llog.Info("foo")
|
||||||
|
|
||||||
|
err := json.Unmarshal(buffer.Bytes(), &firstFields)
|
||||||
|
assert.NoError(t, err, "should have decoded first message")
|
||||||
|
|
||||||
|
buffer.Reset()
|
||||||
|
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
llog.Info("bar")
|
||||||
|
|
||||||
|
err = json.Unmarshal(buffer.Bytes(), &secondFields)
|
||||||
|
assert.NoError(t, err, "should have decoded second message")
|
||||||
|
|
||||||
|
assert.NotEqual(t, firstFields["time"], secondFields["time"], "timestamps should not be equal")
|
||||||
|
}
|
||||||
|
|
||||||
func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
|
func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
Loading…
Reference in New Issue