From c88f8de1fee0d8ecbc5649f4e4f12c96a22b21da Mon Sep 17 00:00:00 2001 From: tbunyk Date: Mon, 18 Mar 2019 16:07:31 +0200 Subject: [PATCH] Add Bytes() method to Entry, and use it to avoid double type cast --- entry.go | 7 ++++++- hooks/writer/writer.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/entry.go b/entry.go index 3d2c9e0..7e8ad25 100644 --- a/entry.go +++ b/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 // formatter. func (entry *Entry) String() (string, error) { - serialized, err := entry.Logger.Formatter.Format(entry) + serialized, err := entry.Bytes() if err != nil { return "", err } diff --git a/hooks/writer/writer.go b/hooks/writer/writer.go index 0532dee..1160c79 100644 --- a/hooks/writer/writer.go +++ b/hooks/writer/writer.go @@ -15,11 +15,11 @@ type Hook struct { // 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 func (hook *Hook) Fire(entry *log.Entry) error { - line, err := entry.String() + line, err := entry.Bytes() if err != nil { return err } - _, err = hook.Writer.Write([]byte(line)) + _, err = hook.Writer.Write(line) return err }