Ver código fonte

Escape strings with quotes (#9)

asib 9 anos atrás
pai
commit
7355c05c86
2 arquivos alterados com 12 adições e 3 exclusões
  1. 3 3
      log/log.go
  2. 9 0
      log/log_test.go

+ 3 - 3
log/log.go

@@ -387,9 +387,9 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
 				if format == "json" {
 					buf.WriteString(message[1:])
 				} else {
-					buf.WriteString(`"message":"`)
-					buf.WriteString(message)
-					buf.WriteString(`"}`)
+					buf.WriteString(`"message":`)
+					buf.WriteString(strconv.Quote(message))
+					buf.WriteString(`}`)
 				}
 			} else {
 				// Text header

+ 9 - 0
log/log_test.go

@@ -110,6 +110,15 @@ func TestJSON(t *testing.T) {
 	assert.Contains(t, b.String(), `"name":"value"`)
 }
 
+func TestStringWithQuotes(t *testing.T) {
+	l := New("test")
+	b := new(bytes.Buffer)
+	l.SetOutput(b)
+	l.SetLevel(DEBUG)
+	l.Debugf("Content-Type: %q", "")
+	assert.Contains(t, b.String(), `"message":"Content-Type: \"\""`)
+}
+
 func BenchmarkLog(b *testing.B) {
 	l := New("test")
 	l.SetOutput(new(bytes.Buffer))