Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
fix(logger): properly parse incoming syslog messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Fisher committed Sep 3, 2014
1 parent 8e21710 commit 0c66ecd
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 305 deletions.
16 changes: 12 additions & 4 deletions logger/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
FROM deis/base:latest
MAINTAINER OpDemand <info@opdemand.com>

# install go dependencies
RUN apt-get update && apt-get install -yq \
git \
bzr \
mercurial \
--no-install-recommends

# install go runtime
RUN wget -qO- https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz | tar -C /usr/local -xz

Expand All @@ -13,12 +20,13 @@ ENV PATH /usr/local/bin:/usr/bin:/bin:/sbin:/usr/local/go/bin
RUN mkdir -p /var/log/deis

# prepare execution environment
WORKDIR /app
WORKDIR /go/src/github.com/deis/deis/logger
CMD ["/app/bin/boot"]
EXPOSE 514

ADD . /app
ADD bin/ /app/bin/
ADD . /go/src/github.com/deis/deis/logger

# compile the binary
RUN cd /go/src/github.com/deis/deis/logger/syslogd && go install -v .
# install dependencies
RUN go get ./syslog
RUN go install -v ./syslogd
5 changes: 0 additions & 5 deletions logger/syslog/filehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ func (h *FileHandler) checkErr(err error) bool {
if err == nil {
return false
}
if h.l == nil {
log.Print(h.filename, ": ", err)
} else {
h.l.Print(h.filename, ": ", err)
}
return true
}

Expand Down
2 changes: 1 addition & 1 deletion logger/syslog/filehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSetLogger(t *testing.T) {

func TestHandle(t *testing.T) {
fh := NewFileHandler("/tmp/test", 1, func(m *Message) bool { return true }, true)
handle := fh.Handle(&Message{time.Now(), nil, 0, 0, time.Now(), "localhost", "test", "message", "", ""})
handle := fh.Handle(&Message{time.Now(), 0, time.Now(), "localhost", "test", "message"})
if handle == nil {
t.Errorf("expected a handle, got nil")
}
Expand Down
36 changes: 9 additions & 27 deletions logger/syslog/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,28 @@ package syslog

import (
"fmt"
"net"
"log/syslog"
"time"
)

// Message defines an RFC 3164 syslog message.
type Message struct {
Time time.Time // time the message was logged
Source net.Addr // source address of the log message
Facility // facility tag (see type Facility)
Severity // severity tag (see type Severity)
Timestamp time.Time // optional
Hostname string // optional
Tag string // message tag as defined in RFC 3164
Content string // message content as defined in RFC 3164
Tag1 string // alternate message tag (white rune as separator)
Content1 string // alternate message content (white rune as separator)
}

// NetSrc only network part of Source as string (IP for UDP or Name for UDS)
func (m *Message) NetSrc() string {
switch a := m.Source.(type) {
case *net.UDPAddr:
return a.IP.String()
case *net.UnixAddr:
return a.Name
case *net.TCPAddr:
return a.IP.String()
}
// Unknown type
return m.Source.String()
Time time.Time
Priority syslog.Priority
Timestamp time.Time
Hostname string
Tag string
Content string
}

// String returns the Message in a string format. This satisfies the fmt.Stringer
// interface.
func (m *Message) String() string {
timeLayout := "2006-01-02 15:04:05"
return fmt.Sprintf(
"%s %s %s%s",
"<%d>%s %s: %s",
m.Priority,
m.Time.Format(timeLayout),
m.Hostname,
m.Tag,
m.Content,
)
Expand Down
68 changes: 2 additions & 66 deletions logger/syslog/message_test.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,15 @@
package syslog

import (
"net"
"testing"
"time"
)

func TestMessageNetSrc(t *testing.T) {
tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234")

if err != nil {
t.Error("could not resolve TCP address")
}

m := &Message{time.Now(), tcpAddress, 0, 0, time.Now(), "", "", "", "", ""}
if m.NetSrc() != "127.0.0.1" {
t.Errorf("expected 127.0.0.1, got %s", m.NetSrc())
}

udpAddress, err := net.ResolveUDPAddr("udp", "localhost:1234")

if err != nil {
t.Error("could not resolve UDP address")
}

m.Source = udpAddress
if m.NetSrc() != "127.0.0.1" {
t.Errorf("expected 127.0.0.1, got %s", m.NetSrc())
}

unixAddress, err := net.ResolveUnixAddr("unix", "/tmp/str")

if err != nil {
t.Error("could not resolve unix address")
}

m.Source = unixAddress
if m.NetSrc() != "/tmp/str" {
t.Errorf("expected /tmp/str, got %s", m.NetSrc())
}

unknownAddress, err := net.ResolveIPAddr("ip4", "localhost")

if err != nil {
t.Error("could not resolve unknown address")
}

m.Source = unknownAddress
if m.NetSrc() != "127.0.0.1" {
t.Errorf("expected 127.0.0.1, got %s", m.NetSrc())
}
}

func TestMessageFormat(t *testing.T) {
tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234")

if err != nil {
t.Errorf("could not resolve TCP address")
}

m := &Message{
time.Now(),
tcpAddress,
0,
0,
time.Now(),
"localhost",
"TEST",
"hello world",
"",
"",
}
m := &Message{time.Now(), 34, time.Now(), "localhost", "test", "hello world"}

timeLayout := "2006-01-02 15:04:05"
expectedOutput := m.Time.Format(timeLayout) + " localhost TESThello world"
expectedOutput := "<34>" + m.Time.Format(timeLayout) + " test: hello world"
if m.String() != expectedOutput {
t.Errorf("expected '" + expectedOutput + "', got '" + m.String() + "'.")
}
Expand Down
110 changes: 0 additions & 110 deletions logger/syslog/priority.go

This file was deleted.

27 changes: 0 additions & 27 deletions logger/syslog/priority_test.go

This file was deleted.

Loading

0 comments on commit 0c66ecd

Please sign in to comment.