-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a359e0b
commit 73f2839
Showing
24 changed files
with
587 additions
and
42 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
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,20 @@ | ||
package benchmark_formatter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gol4ng/logger" | ||
"github.com/gol4ng/logger/formatter" | ||
) | ||
|
||
func BenchmarkGelfFormatter(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
gelfFormatter := formatter.NewGelf() | ||
e := logger.Entry{Message: "This log message is really logged.", Level: logger.InfoLevel} | ||
|
||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
gelfFormatter.Format(e) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package example_formatter_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"bou.ke/monkey" | ||
|
||
"github.com/gol4ng/logger" | ||
"github.com/gol4ng/logger/formatter" | ||
) | ||
|
||
func ExampleGelfFormatter() { | ||
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) }) | ||
monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil }) | ||
defer monkey.UnpatchAll() | ||
|
||
gelfFormatter := formatter.NewGelf() | ||
|
||
fmt.Println(gelfFormatter.Format(logger.Entry{Message: "My log message", Level: logger.InfoLevel, Context: logger.NewContext().Add("my key", "my_value")})) | ||
|
||
//Output: | ||
//{"version":"1.1","host":"my_fake_hostname","level":6,"timestamp":513216000.000,"short_message":"My log message","full_message":"<info> My log message [ <my key:my_value> ]","_my_key":"my_value"} | ||
} |
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,22 @@ | ||
package example_formatter_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gol4ng/logger" | ||
"github.com/gol4ng/logger/formatter" | ||
) | ||
|
||
func ExampleJsonFormatter() { | ||
jsonFormatter := formatter.NewJSONEncoder() | ||
|
||
fmt.Println(jsonFormatter.Format( | ||
logger.Entry{ | ||
Message: "My log message", | ||
Level: logger.InfoLevel, | ||
Context: logger.NewContext().Add("my_key", "my_value"), | ||
}, | ||
)) | ||
//Output: | ||
//{"Message":"My log message","Level":6,"Context":{"my_key":"my_value"}} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package formatter | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gol4ng/logger" | ||
) | ||
|
||
const ( | ||
// Version is the current Gelf version | ||
Version string = "1.1" | ||
) | ||
|
||
// Gelf formatter will transform Entry into Gelf format | ||
type Gelf struct { | ||
hostname string | ||
version string | ||
} | ||
|
||
// Format will return Entry as gelf | ||
func (g *Gelf) Format(entry logger.Entry) string { | ||
builder := &strings.Builder{} | ||
|
||
builder.WriteString(`{"version":"`) | ||
builder.WriteString(g.version) | ||
|
||
builder.WriteString(`","host":"`) | ||
builder.WriteString(g.hostname) | ||
|
||
builder.WriteString(`","level":`) | ||
builder.WriteString(strconv.Itoa(int(entry.Level))) | ||
|
||
builder.WriteString(`,"timestamp":`) | ||
builder.WriteString(strconv.FormatFloat(float64(time.Now().UnixNano())/1e9, 'f', 3, 64)) | ||
|
||
builder.WriteString(`,"short_message":"`) | ||
builder.WriteString(entry.Message) | ||
|
||
builder.WriteString(`","full_message":"`) | ||
logger.EntryToString(entry, builder) | ||
builder.WriteString(`"`) | ||
|
||
if entry.Context != nil { | ||
for name, field := range *entry.Context { | ||
builder.WriteString(`,"_`) | ||
builder.WriteString(strings.Replace(name, " ", "_", -1)) | ||
builder.WriteString(`":`) | ||
d, _ := json.Marshal(field.Value) | ||
builder.Write(d) | ||
} | ||
} | ||
|
||
builder.WriteString("}") | ||
|
||
return builder.String() | ||
} | ||
|
||
// NewGelf will create a new Gelf formatter | ||
func NewGelf() *Gelf { | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return &Gelf{hostname: hostname, version: Version} | ||
} | ||
|
||
// GelfTCPFormatter will decorate Gelf formatter in order to add null byte between each entry | ||
// http://docs.graylog.org/en/3.0/pages/gelf.html#gelf-via-tcp | ||
type GelfTCPFormatter struct { | ||
*Gelf | ||
} | ||
|
||
// Format will return Entry as gelf TCP | ||
func (g *GelfTCPFormatter) Format(entry logger.Entry) string { | ||
return g.Gelf.Format(entry) + "\x00" | ||
} | ||
|
||
// NewGelfTCP will create a new GelfTCPFormatter | ||
func NewGelfTCP() *GelfTCPFormatter { | ||
return &GelfTCPFormatter{Gelf: NewGelf()} | ||
} |
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,27 @@ | ||
package formatter_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"bou.ke/monkey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gol4ng/logger" | ||
"github.com/gol4ng/logger/formatter" | ||
) | ||
|
||
func TestGelf_Format(t *testing.T) { | ||
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) }) | ||
monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil }) | ||
defer monkey.UnpatchAll() | ||
|
||
gelf := formatter.NewGelf() | ||
|
||
assert.Equal( | ||
t, | ||
"{\"version\":\"1.1\",\"host\":\"my_fake_hostname\",\"level\":6,\"timestamp\":513216000.000,\"short_message\":\"My log message\",\"full_message\":\"<info> My log message [ <my key:my_value> ]\",\"_my_key\":\"my_value\"}", | ||
gelf.Format(logger.Entry{Message: "My log message", Level: logger.InfoLevel, Context: logger.NewContext().Add("my key", "my_value")}), | ||
) | ||
} |
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
Oops, something went wrong.