]> Cypherpunks repositories - gostls13.git/commitdiff
log/syslog: use alternate format for logging to local syslog daemon
authorRuss Cox <rsc@golang.org>
Mon, 9 Sep 2013 20:17:44 +0000 (16:17 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 9 Sep 2013 20:17:44 +0000 (16:17 -0400)
Fixes #5803.
Is it correct behavior? Who knows.

R=golang-dev, bradfitz, jgc
CC=golang-dev, jgc
https://golang.org/cl/13248048

src/pkg/log/syslog/syslog.go
src/pkg/log/syslog/syslog_unix.go

index 99c266ac8e19d1f501b98300d26adf34da5c5027..0cbfa9011b8e0fa0dd9648634797bec0816bd6c1 100644 (file)
@@ -103,7 +103,8 @@ type serverConn interface {
 }
 
 type netConn struct {
-       conn net.Conn
+       local bool
+       conn  net.Conn
 }
 
 // New establishes a new connection to the system log daemon.  Each
@@ -163,7 +164,7 @@ func (w *Writer) connect() (err error) {
                var c net.Conn
                c, err = net.Dial(w.network, w.raddr)
                if err == nil {
-                       w.conn = netConn{c}
+                       w.conn = &netConn{conn: c}
                        if w.hostname == "" {
                                w.hostname = c.LocalAddr().String()
                        }
@@ -282,7 +283,17 @@ func (w *Writer) write(p Priority, msg string) (int, error) {
        return len(msg), nil
 }
 
-func (n netConn) writeString(p Priority, hostname, tag, msg, nl string) error {
+func (n *netConn) writeString(p Priority, hostname, tag, msg, nl string) error {
+       if n.local {
+               // Compared to the network form below, the changes are:
+               //      1. Use time.Stamp instead of time.RFC3339.
+               //      2. Drop the hostname field from the Fprintf.
+               timestamp := time.Now().Format(time.Stamp)
+               _, err := fmt.Fprintf(n.conn, "<%d>%s %s[%d]: %s%s",
+                       p, timestamp,
+                       tag, os.Getpid(), msg, nl)
+               return err
+       }
        timestamp := time.Now().Format(time.RFC3339)
        _, err := fmt.Fprintf(n.conn, "<%d>%s %s %s[%d]: %s%s",
                p, timestamp, hostname,
@@ -290,7 +301,7 @@ func (n netConn) writeString(p Priority, hostname, tag, msg, nl string) error {
        return err
 }
 
-func (n netConn) close() error {
+func (n *netConn) close() error {
        return n.conn.Close()
 }
 
index 1716d60feaad31f1d89f1f54fd98d0bf49acadcb..28a294af96387eb96a2546522b54df304f62723d 100644 (file)
@@ -23,7 +23,7 @@ func unixSyslog() (conn serverConn, err error) {
                        if err != nil {
                                continue
                        } else {
-                               return netConn{conn}, nil
+                               return &netConn{conn: conn, local: true}, nil
                        }
                }
        }