entry: document entry and methods

This commit is contained in:
Simon Eskildsen 2014-07-26 21:37:06 -04:00
parent 40069a98d6
commit cfddc66325
1 changed files with 12 additions and 1 deletions

View File

@ -8,9 +8,15 @@ import (
"time" "time"
) )
// An entry is the final or intermediate Logrus logging entry. It containts all
// the fields passed with WithField{,s}. It's finally logged when Debug, Info,
// Warn, Error, Fatal or Panic is called on it. These objects can be reused and
// passed around as much as you wish to avoid field duplication.
type Entry struct { type Entry struct {
Logger *Logger Logger *Logger
Data Fields
// Contains all the fields set by the user.
Data Fields
// Time at which the log entry was created // Time at which the log entry was created
Time time.Time Time time.Time
@ -32,11 +38,14 @@ func NewEntry(logger *Logger) *Entry {
} }
} }
// Returns a reader for the entry, which is a proxy to the formatter.
func (entry *Entry) Reader() (*bytes.Buffer, error) { func (entry *Entry) Reader() (*bytes.Buffer, error) {
serialized, err := entry.Logger.Formatter.Format(entry) serialized, err := entry.Logger.Formatter.Format(entry)
return bytes.NewBuffer(serialized), err return bytes.NewBuffer(serialized), err
} }
// Returns the string representation from the reader and ultimately the
// formatter.
func (entry *Entry) String() (string, error) { func (entry *Entry) String() (string, error) {
reader, err := entry.Reader() reader, err := entry.Reader()
if err != nil { if err != nil {
@ -46,10 +55,12 @@ func (entry *Entry) String() (string, error) {
return reader.String(), err return reader.String(), err
} }
// Add a single field to the Entry.
func (entry *Entry) WithField(key string, value interface{}) *Entry { func (entry *Entry) WithField(key string, value interface{}) *Entry {
return entry.WithFields(Fields{key: value}) return entry.WithFields(Fields{key: value})
} }
// Add a map of fields to the Entry.
func (entry *Entry) WithFields(fields Fields) *Entry { func (entry *Entry) WithFields(fields Fields) *Entry {
data := Fields{} data := Fields{}
for k, v := range entry.Data { for k, v := range entry.Data {