From c09649890fd48f9854120999a109ba968596a021 Mon Sep 17 00:00:00 2001 From: John Graham-Cumming Date: Fri, 4 Jan 2013 10:21:43 -0500 Subject: [PATCH] log/syslog: remove RFC5424 version number for greater compatibility RFC5424 specifies a version number (currently 1) after the facility and severity in a syslog message (e.g. <7>1 TIMESTAMP ...). This causes rsyslog to fail to parse syslog message because the rest of the message is not fully compliant with RFC5424. For the widest compatibility, drop the version (messages are in the RFC3164 BSD syslog format (e.g. <7>TIMESTAMP ...). Have tested this with syslog-ng, rsyslog and syslogd. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/7036050 --- src/pkg/log/syslog/syslog.go | 4 ++-- src/pkg/log/syslog/syslog_test.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/pkg/log/syslog/syslog.go b/src/pkg/log/syslog/syslog.go index c4ad12ffcd1..98b9c5f6e87 100644 --- a/src/pkg/log/syslog/syslog.go +++ b/src/pkg/log/syslog/syslog.go @@ -202,14 +202,14 @@ func (w *Writer) writeString(p Priority, s string) (int, error) { } // writeString: generates and writes a syslog formatted string. The -// format is as follows: 1 TIMESTAMP HOSTNAME TAG[PID]: MSG +// format is as follows: TIMESTAMP HOSTNAME TAG[PID]: MSG func (n netConn) writeString(p Priority, hostname, tag, msg string) (int, error) { nl := "" if len(msg) == 0 || msg[len(msg)-1] != '\n' { nl = "\n" } timestamp := time.Now().Format(time.RFC3339) - if _, err := fmt.Fprintf(n.conn, "<%d>1 %s %s %s[%d]: %s%s", p, timestamp, hostname, + if _, err := fmt.Fprintf(n.conn, "<%d>%s %s %s[%d]: %s%s", p, timestamp, hostname, tag, os.Getpid(), msg, nl); err != nil { return 0, err } diff --git a/src/pkg/log/syslog/syslog_test.go b/src/pkg/log/syslog/syslog_test.go index 67d7103ee44..d1fb1b23835 100644 --- a/src/pkg/log/syslog/syslog_test.go +++ b/src/pkg/log/syslog/syslog_test.go @@ -104,16 +104,15 @@ func TestUDPDial(t *testing.T) { } msg := "udp test" l.Info(msg) - expected := fmt.Sprintf("<%d>1 ", LOG_USER+LOG_INFO) + "%s %s syslog_test[%d]: udp test\n" + expected := fmt.Sprintf("<%d>", LOG_USER+LOG_INFO) + "%s %s syslog_test[%d]: udp test\n" rcvd := <-done var parsedHostname, timestamp string var pid int if hostname, err := os.Hostname(); err != nil { t.Fatalf("Error retrieving hostname") } else { - if n, err := fmt.Sscanf(rcvd, expected, ×tamp, &parsedHostname, &pid); n != 3 || - err != nil || hostname != parsedHostname { - t.Fatalf("s.Info() = '%q', didn't match '%q'", rcvd, expected) + if n, err := fmt.Sscanf(rcvd, expected, ×tamp, &parsedHostname, &pid); n != 3 || err != nil || hostname != parsedHostname { + t.Fatalf("'%q', didn't match '%q' (%d, %s)", rcvd, expected, n, err) } } } @@ -146,12 +145,13 @@ func TestWrite(t *testing.T) { t.Fatalf("WriteString() failed: %s", err) } rcvd := <-done - test.exp = fmt.Sprintf("<%d>1 ", test.pri) + test.exp + test.exp = fmt.Sprintf("<%d>", test.pri) + test.exp var parsedHostname, timestamp string var pid int - if n, err := fmt.Sscanf(rcvd, test.exp, ×tamp, &parsedHostname, &pid); n != 3 || - err != nil || hostname != parsedHostname { - t.Fatalf("s.Info() = '%q', didn't match '%q'", rcvd, test.exp) + if n, err := fmt.Sscanf(rcvd, test.exp, ×tamp, &parsedHostname, + &pid); n != 3 || err != nil || hostname != parsedHostname { + t.Fatalf("'%q', didn't match '%q' (%d %s)", rcvd, test.exp, + n, err) } } }