mirror of https://github.com/sirupsen/logrus.git
Add Bytes() method to Entry, and use it to avoid double type cast
This commit is contained in:
parent
470f2e08fc
commit
c88f8de1fe
7
entry.go
7
entry.go
|
@ -85,10 +85,15 @@ func NewEntry(logger *Logger) *Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the bytes representation of this entry from the formatter.
|
||||||
|
func (entry *Entry) Bytes() ([]byte, error) {
|
||||||
|
return entry.Logger.Formatter.Format(entry)
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the string representation from the reader and ultimately the
|
// Returns the string representation from the reader and ultimately the
|
||||||
// formatter.
|
// formatter.
|
||||||
func (entry *Entry) String() (string, error) {
|
func (entry *Entry) String() (string, error) {
|
||||||
serialized, err := entry.Logger.Formatter.Format(entry)
|
serialized, err := entry.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,11 @@ type Hook struct {
|
||||||
// Fire will be called when some logging function is called with current hook
|
// Fire will be called when some logging function is called with current hook
|
||||||
// It will format log entry to string and write it to appropriate writer
|
// It will format log entry to string and write it to appropriate writer
|
||||||
func (hook *Hook) Fire(entry *log.Entry) error {
|
func (hook *Hook) Fire(entry *log.Entry) error {
|
||||||
line, err := entry.String()
|
line, err := entry.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = hook.Writer.Write([]byte(line))
|
_, err = hook.Writer.Write(line)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue