Skip to content

Commit 6512cfd

Browse files
author
Matthew Fisher
committed
fix(logspout): use custom log format
Go's syslog package hardcodes a lot of the log message format. This is very difficult to customize for our own use. For example, we want to follow a different timestamp format than RFC3339. We also want to remove the hostname from the logs since we don't want users to "know" where each container is being run from in the logs. In our case, though, the hostname is the container ID for deis-logspout on that host which is useless for the user, anyways. Connecting to the syslog server directly allows us to format the message in a sensible manner.
1 parent e39edb6 commit 6512cfd

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

logspout/logspout.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package main
22

33
import (
44
"fmt"
5-
"io"
65
"log"
7-
"log/syslog"
6+
"net"
87
"net/http"
98
"net/url"
109
"os"
10+
"regexp"
1111
"strconv"
1212
"strings"
1313
"time"
@@ -62,10 +62,31 @@ func syslogStreamer(target Target, types []string, logstream chan *Log) {
6262
if typestr != ",," && !strings.Contains(typestr, logline.Type) {
6363
continue
6464
}
65-
tag := logline.Name + target.AppendTag
66-
remote, err := syslog.Dial("udp", target.Addr, syslog.LOG_USER|syslog.LOG_INFO, tag)
65+
tag, pid := getLogName(logline.Name)
66+
conn, err := net.Dial("udp", target.Addr)
6767
assert(err, "syslog")
68-
io.WriteString(remote, logline.Data)
68+
// HACK: Go's syslog package hardcodes the log format, so let's send our own message
69+
_, err = fmt.Fprintf(conn,
70+
"%s %s[%s]: %s",
71+
time.Now().Format("2006-01-02 15:04:05"),
72+
tag,
73+
pid,
74+
logline.Data)
75+
assert(err, "syslog")
76+
}
77+
}
78+
79+
// getLogName returns a custom tag and PID for containers that
80+
// match Deis' specific application name format. Otherwise,
81+
// it returns the original name and 1 as the PID.
82+
func getLogName(name string) (string, string) {
83+
// example regex that should match: go_v2.web.1
84+
r := regexp.MustCompile(`(^[a-z0-9-]+)_(v[0-9]+)\.([a-z-_]+\.[0-9]+)$`)
85+
match := r.FindStringSubmatch(name)
86+
if match == nil {
87+
return name, "1"
88+
} else {
89+
return match[1], match[3]
6990
}
7091
}
7192

0 commit comments

Comments
 (0)