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

Commit

Permalink
Address the review comments.
Browse files Browse the repository at this point in the history
Make the truncating function not exportable outside package.
Improve name for AppName max chars constant.
Put constant local to the formatter module.
  • Loading branch information
scorptec68 committed Jan 18, 2018
1 parent c5e276e commit c940732
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 0 additions & 1 deletion constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type Priority int

const severityMask = 0x07
const facilityMask = 0xf8
const appNameMax = 48 // limit to 48 chars as per RFC5424

const (
// Severity.
Expand Down
6 changes: 4 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"
)

const appNameMaxLength = 48 // limit to 48 chars as per RFC5424

// 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.
Expand Down Expand Up @@ -38,7 +40,7 @@ func RFC3164Formatter(p Priority, hostname, tag, content string) string {
}

// if string's length is greater than max, then use the last part
func TruncateStartStr(s string, max int) string {
func truncateStartStr(s string, max int) string {
if (len(s) > max) {
return s[len(s) - max:]
}
Expand All @@ -49,7 +51,7 @@ func TruncateStartStr(s string, max int) string {
func RFC5424Formatter(p Priority, hostname, tag, content string) string {
timestamp := time.Now().Format(time.RFC3339)
pid := os.Getpid()
appName := TruncateStartStr(os.Args[0], appNameMax)
appName := truncateStartStr(os.Args[0], appNameMaxLength)
msg := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
p, 1, timestamp, hostname, appName, pid, tag, content)
return msg
Expand Down
6 changes: 3 additions & 3 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ func TestRFC3164Formatter(t *testing.T) {
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", TruncateStartStr(os.Args[0], appNameMax),
LOG_ERR, 1, time.Now().Format(time.RFC3339), "hostname", truncateStartStr(os.Args[0], appNameMaxLength),
os.Getpid(), "tag", "content")
if out != expected {
t.Errorf("expected %v got %v", expected, out)
}
}

func TestTruncateStartStr(t *testing.T) {
out := TruncateStartStr("abcde", 3)
out := truncateStartStr("abcde", 3)
if strings.Compare(out, "cde" ) != 0 {
t.Errorf("expected \"cde\" got %v", out)
}
out = TruncateStartStr("abcde", 5)
out = truncateStartStr("abcde", 5)
if strings.Compare(out, "abcde" ) != 0 {
t.Errorf("expected \"abcde\" got %v", out)
}
Expand Down

0 comments on commit c940732

Please sign in to comment.