From a5e6e197a631bac8485351dfa28d65fd8051ef9a Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Thu, 13 Oct 2016 00:14:09 +0100 Subject: [PATCH] trace: Quote errors before logging them Errors can contain newlines, in particular this is common in messages returned by remote SMTP servers. This patch quotes them before logging, they don't interfere with the log. Note the tracer itself can handle this just fine. --- internal/trace/trace.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/trace/trace.go b/internal/trace/trace.go index 6f705b2..ccec62b 100644 --- a/internal/trace/trace.go +++ b/internal/trace/trace.go @@ -45,11 +45,6 @@ func (t *Trace) Debugf(format string, a ...interface{}) { } } -func quote(s string) string { - qs := strconv.Quote(s) - return qs[1 : len(qs)-1] -} - func (t *Trace) SetError() { t.t.SetError() } @@ -60,7 +55,8 @@ func (t *Trace) Errorf(format string, a ...interface{}) error { t.t.LazyPrintf("error: %v", err) if glog.V(0) { - msg := fmt.Sprintf("%s %s: error: %v", t.family, t.title, err) + msg := fmt.Sprintf("%s %s: error: %s", t.family, t.title, + quote(err.Error())) glog.InfoDepth(1, msg) } return err @@ -71,7 +67,8 @@ func (t *Trace) Error(err error) error { t.t.LazyPrintf("error: %v", err) if glog.V(0) { - msg := fmt.Sprintf("%s %s: error: %v", t, t.family, t.title, err) + msg := fmt.Sprintf("%s %s: error: %v", t.family, t.title, + quote(err.Error())) glog.InfoDepth(1, msg) } return err @@ -116,8 +113,14 @@ func (e *EventLog) Errorf(format string, a ...interface{}) error { e.e.Errorf("error: %v", err) if glog.V(0) { - msg := fmt.Sprintf("%s %s: error: %v", e.family, e.title, err) + msg := fmt.Sprintf("%s %s: error: %s", e.family, e.title, + quote(err.Error())) glog.InfoDepth(1, msg) } return err } + +func quote(s string) string { + qs := strconv.Quote(s) + return qs[1 : len(qs)-1] +}