This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added configurable formatters and framers.
Default behavior is backwards compatible (ie, it remains non-compliant). But now we support RFC 3164 and RFC 5424 formats, and RFC 5425 message framing.
- Loading branch information
Showing
9 changed files
with
268 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package srslog | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
) | ||
|
||
// Formatter is a type of function that takes the consituent parts of a | ||
// syslog message and returns a formatted string. A different Formatter is | ||
// defined for each different syslog protocol we support. | ||
type Formatter func(p Priority, hostname, tag, content string) string | ||
|
||
// DefaultFormatter is the original format supported by the Go syslog package, | ||
// and is a non-compliant amalgamation of 3164 and 5424 that is intended to | ||
// maximize compatibility. | ||
func DefaultFormatter(p Priority, hostname, tag, content string) string { | ||
timestamp := time.Now().Format(time.RFC3339) | ||
msg := fmt.Sprintf("<%d> %s %s %s[%d]: %s", | ||
p, timestamp, hostname, tag, os.Getpid(), content) | ||
return msg | ||
} | ||
|
||
// UnixFormatter omits the hostname, because it is only used locally. | ||
func UnixFormatter(p Priority, hostname, tag, content string) string { | ||
timestamp := time.Now().Format(time.Stamp) | ||
msg := fmt.Sprintf("<%d>%s %s[%d]: %s", | ||
p, timestamp, tag, os.Getpid(), content) | ||
return msg | ||
} | ||
|
||
// RFC3164Formatter provides an RFC 3164 compliant message. | ||
func RFC3164Formatter(p Priority, hostname, tag, content string) string { | ||
timestamp := time.Now().Format(time.Stamp) | ||
msg := fmt.Sprintf("<%d> %s %s %s[%d]: %s", | ||
p, timestamp, hostname, tag, os.Getpid(), content) | ||
return msg | ||
} | ||
|
||
// RFC5424Formatter provides an RFC 5424 compliant message. | ||
func RFC5424Formatter(p Priority, hostname, tag, content string) string { | ||
timestamp := time.Now().Format(time.RFC3339) | ||
pid := os.Getpid() | ||
appName := os.Args[0] | ||
msg := fmt.Sprintf("<%d>%d %s %s %s %d %s %s", | ||
p, 1, timestamp, hostname, appName, pid, tag, content) | ||
return msg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package srslog | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDefaultFormatter(t *testing.T) { | ||
out := DefaultFormatter(LOG_ERR, "hostname", "tag", "content") | ||
expected := fmt.Sprintf("<%d> %s %s %s[%d]: %s", | ||
LOG_ERR, time.Now().Format(time.RFC3339), "hostname", "tag", os.Getpid(), "content") | ||
if out != expected { | ||
t.Errorf("expected %v got %v", expected, out) | ||
} | ||
} | ||
|
||
func TestUnixFormatter(t *testing.T) { | ||
out := UnixFormatter(LOG_ERR, "hostname", "tag", "content") | ||
expected := fmt.Sprintf("<%d>%s %s[%d]: %s", | ||
LOG_ERR, time.Now().Format(time.Stamp), "tag", os.Getpid(), "content") | ||
if out != expected { | ||
t.Errorf("expected %v got %v", expected, out) | ||
} | ||
} | ||
|
||
func TestRFC3164Formatter(t *testing.T) { | ||
out := RFC3164Formatter(LOG_ERR, "hostname", "tag", "content") | ||
expected := fmt.Sprintf("<%d> %s %s %s[%d]: %s", | ||
LOG_ERR, time.Now().Format(time.Stamp), "hostname", "tag", os.Getpid(), "content") | ||
if out != expected { | ||
t.Errorf("expected %v got %v", expected, out) | ||
} | ||
} | ||
|
||
func TestRFC5424Formatter(t *testing.T) { | ||
out := RFC5424Formatter(LOG_ERR, "hostname", "tag", "content") | ||
expected := fmt.Sprintf("<%d>%d %s %s %s %d %s %s", | ||
LOG_ERR, 1, time.Now().Format(time.RFC3339), "hostname", os.Args[0], os.Getpid(), "tag", "content") | ||
if out != expected { | ||
t.Errorf("expected %v got %v", expected, out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package srslog | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Framer is a type of function that takes an input string (typically an | ||
// already-formatted syslog message) and applies "message framing" to it. We | ||
// have different framers because different versions of the syslog protocol | ||
// and its transport requirements define different framing behavior. | ||
type Framer func(in string) string | ||
|
||
// DefaultFramer does nothing, since there is no framing to apply. This is | ||
// the original behavior of the Go syslog package, and is also typically used | ||
// for UDP syslog. | ||
func DefaultFramer(in string) string { | ||
return in | ||
} | ||
|
||
// RFC5425MessageLengthFramer prepends the message length to the front of the | ||
// provided message, as defined in RFC 5425. This is required by syslog-ng, | ||
// and is used with either RFC 3164 or 5424, over TCP+TLS. | ||
func RFC5425MessageLengthFramer(in string) string { | ||
return fmt.Sprintf("%d %s", len(in), in) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package srslog | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDefaultFramer(t *testing.T) { | ||
out := DefaultFramer("input message") | ||
if out != "input message" { | ||
t.Errorf("should match the input message") | ||
} | ||
} | ||
|
||
func TestRFC5425MessageLengthFramer(t *testing.T) { | ||
out := RFC5425MessageLengthFramer("input message") | ||
if out != "13 input message" { | ||
t.Errorf("should prepend the input message length") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters