Skip to content
This repository has been archived by the owner on Mar 18, 2019. It is now read-only.

Commit

Permalink
Set the hostname of syslog messages if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
morganxf committed May 14, 2018
1 parent f6152a1 commit 13efd00
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (w *Writer) SetFramer(f Framer) {
w.framer = f
}

// SetHostname changes the hostname for syslog messages if needed.
func (w *Writer) SetHostname(hostname string) {
w.hostname = hostname
}

// Write sends a log message to the syslog daemon using the default priority
// passed into `srslog.New` or the `srslog.Dial*` functions.
func (w *Writer) Write(b []byte) (int, error) {
Expand Down
32 changes: 32 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ func TestWriteAndRetryFails(t *testing.T) {
}
}

func TestSetHostname(t *testing.T) {
customHostname := "kubernetesCluster"
expected := customHostname

done := make(chan string)
addr, sock, srvWG := startServer("udp", "", done)
defer sock.Close()
defer srvWG.Wait()

w := Writer{
priority: LOG_ERR,
network: "udp",
raddr: addr,
}

_, err := w.connect()
if err != nil {
t.Errorf("failed to connect: %v", err)
}
defer w.Close()

if strings.Split(w.hostname, ":")[0] != "127.0.0.1" {
t.Errorf("expected hostname: %s, got %s", "127.0.0.1", strings.Split(w.hostname, ":")[0])
}

w.SetHostname(customHostname)
if w.hostname != expected {
t.Errorf("expected hostname: %s, got %s", expected, w.hostname)
}
<-done
}

func TestWriteFormatters(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 13efd00

Please sign in to comment.