diff --git a/README.md b/README.md index d0b6b23..f76c8b2 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,15 @@ fluent-logger-golang ## How to install -``` -go get github.com/fluent/fluent-logger-golang/fluent +```bash +go get github.com/fluent/fluent-logger-golang/fluent@latest ``` ## Usage Install the package with `go get` and use `import` to include it in your project. -``` +```go import "github.com/fluent/fluent-logger-golang/fluent" ``` @@ -26,27 +26,32 @@ import "github.com/fluent/fluent-logger-golang/fluent" package main import ( - "github.com/fluent/fluent-logger-golang/fluent" - "fmt" - //"time" + "fmt" + "time" + + "github.com/fluent/fluent-logger-golang/fluent" ) func main() { - logger, err := fluent.New(fluent.Config{}) - if err != nil { - fmt.Println(err) - } - defer logger.Close() - tag := "myapp.access" - var data = map[string]string{ - "foo": "bar", - "hoge": "hoge", - } - error := logger.Post(tag, data) - // error := logger.PostWithTime(tag, time.Now(), data) - if error != nil { - panic(error) - } + logger, err := fluent.New(fluent.Config{}) + if err != nil { + fmt.Println(err) + } + defer logger.Close() + tag := "myapp.access" + data := map[string]string{ + "foo": "bar", + "hoge": "hoge", + } + err = logger.Post(tag, data) + if err != nil { + panic(err) + } + + err = logger.PostWithTime(tag, time.Now(), data) + if err != nil { + panic(err) + } } ``` @@ -181,7 +186,7 @@ were involved. Starting v1.8.0, the logger no longer accepts `Fluent.Post()` after `Fluent.Close()`, and instead returns a "Logger already closed" error. ## Tests -``` +```bash go test ``` diff --git a/_examples/main.go b/_examples/main.go index cb3e2e2..77d8d90 100644 --- a/_examples/main.go +++ b/_examples/main.go @@ -5,7 +5,7 @@ import ( "log" "time" - "../fluent" + "github.com/fluent/fluent-logger-golang/fluent" ) func main() { @@ -15,9 +15,10 @@ func main() { } defer logger.Close() tag := "myapp.access" - var data = map[string]string{ + data := map[string]string{ "foo": "bar", - "hoge": "hoge"} + "hoge": "hoge", + } for i := 0; i < 100; i++ { e := logger.Post(tag, data) if e != nil { diff --git a/fluent/fluent.go b/fluent/fluent.go index d33012f..c414f97 100644 --- a/fluent/fluent.go +++ b/fluent/fluent.go @@ -1,8 +1,11 @@ package fluent import ( + "bytes" "context" "crypto/tls" + "encoding/base64" + "encoding/binary" "encoding/json" "errors" "fmt" @@ -16,10 +19,6 @@ import ( "sync/atomic" "time" - "bytes" - "encoding/base64" - "encoding/binary" - "github.com/tinylib/msgp/msgp" ) @@ -323,7 +322,7 @@ func (chunk *MessageChunk) MarshalJSON() ([]byte, error) { if err != nil { return nil, err } - return []byte(fmt.Sprintf("[\"%s\",%d,%s,%s]", chunk.message.Tag, + return []byte(fmt.Sprintf(`["%s",%d,%s,%s]`, chunk.message.Tag, chunk.message.Time, data, option)), err } diff --git a/fluent/fluent_test.go b/fluent/fluent_test.go index c8b33e7..26c1cea 100644 --- a/fluent/fluent_test.go +++ b/fluent/fluent_test.go @@ -105,12 +105,12 @@ func newTestDialer() *testDialer { // conn := d.waitForNextDialing(true, false) // assertReceived(t, // t is *testing.T // conn.waitForNextWrite(true, ""), -// "[\"tag_name\",1482493046,{\"foo\":\"bar\"},{}]") +// `["tag_name",1482493046,{"foo":"bar"},{}]`) // // f.EncodeAndPostData("something_else", time.Unix(1482493050, 0), map[string]string{"bar": "baz"}) // assertReceived(t, // t is *testing.T // conn.waitForNextWrite(true, ""), -// "[\"something_else\",1482493050,{\"bar\":\"baz\"},{}]") +// `["something_else",1482493050,{"bar":"baz"},{}]`) // // In this example, the 1st connection dialing succeeds but the 1st attempt to write the // message is discarded. As the logger discards the connection whenever a message @@ -120,7 +120,7 @@ func newTestDialer() *testDialer { // using assertReceived() to make sure the logger encodes the messages properly. // // Again, the example above is using async mode thus, calls to f and conn are running in -// the same goroutine. However in sync mode, all calls to f.EncodeAndPostData() as well +// the same goroutine. However, in sync mode, all calls to f.EncodeAndPostData() as well // as the logger initialization shall be placed in a separate goroutine or the code // allowing the dialing and writing attempts (eg. waitForNextDialing() & waitForNextWrite()) // would never be reached. @@ -311,7 +311,8 @@ func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) { f, err := New(Config{ FluentNetwork: network, - FluentSocketPath: socketFile}) + FluentSocketPath: socketFile, + }) if err != nil { t.Error(err) return @@ -324,7 +325,8 @@ func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) { network = "unixxxx" fUnknown, err := New(Config{ FluentNetwork: network, - FluentSocketPath: socketFile}) + FluentSocketPath: socketFile, + }) if _, ok := err.(*ErrUnknownNetwork); !ok { t.Errorf("err type: %T", err) } @@ -350,12 +352,12 @@ func Test_MarshalAsMsgpack(t *testing.T) { f := &Fluent{Config: Config{}} tag := "tag" - var data = map[string]string{ + data := map[string]string{ "foo": "bar", - "hoge": "hoge"} + "hoge": "hoge", + } tm := time.Unix(1267867237, 0) result, err := f.EncodeData(tag, tm, data) - if err != nil { t.Error(err) } @@ -383,7 +385,6 @@ func Test_SubSecondPrecision(t *testing.T) { encodedData, err := fluent.EncodeData("tag", timestamp, map[string]string{ "foo": "bar", }) - // Assert no encoding errors and that the timestamp has been encoded into // the message as expected. if err != nil { @@ -402,12 +403,12 @@ func Test_SubSecondPrecision(t *testing.T) { func Test_MarshalAsJSON(t *testing.T) { f := &Fluent{Config: Config{MarshalAsJSON: true}} - var data = map[string]string{ + data := map[string]string{ "foo": "bar", - "hoge": "hoge"} + "hoge": "hoge", + } tm := time.Unix(1267867237, 0) result, err := f.EncodeData("tag", tm, data) - if err != nil { t.Error(err) } @@ -494,14 +495,14 @@ func TestPostWithTime(t *testing.T) { conn := d.waitForNextDialing(true, false) assertReceived(t, conn.waitForNextWrite(true, ""), - "[\"acme.tag_name\",1482493046,{\"foo\":\"bar\"},{}]") + `["acme.tag_name",1482493046,{"foo":"bar"},{}]`) assertReceived(t, conn.waitForNextWrite(true, ""), - "[\"acme.tag_name\",1482493050,{\"fluentd\":\"is awesome\"},{}]") + `["acme.tag_name",1482493050,{"fluentd":"is awesome"},{}]`) assertReceived(t, conn.waitForNextWrite(true, ""), - "[\"acme.tag_name\",1634263200,{\"welcome\":\"to use\"},{}]") + `["acme.tag_name",1634263200,{"welcome":"to use"},{}]`) }) } } @@ -545,7 +546,7 @@ func TestReconnectAndResendAfterTransientFailure(t *testing.T) { conn := d.waitForNextDialing(true, false) assertReceived(t, conn.waitForNextWrite(true, ""), - "[\"tag_name\",1482493046,{\"foo\":\"bar\"},{}]") + `["tag_name",1482493046,{"foo":"bar"},{}]`) // The next write will fail and the next connection dialing will be dropped // to test if the logger is reconnecting as expected. @@ -556,7 +557,7 @@ func TestReconnectAndResendAfterTransientFailure(t *testing.T) { conn = d.waitForNextDialing(true, false) assertReceived(t, conn.waitForNextWrite(true, ""), - "[\"tag_name\",1482493050,{\"fluentd\":\"is awesome\"},{}]") + `["tag_name",1482493050,{"fluentd":"is awesome"},{}]`) }) } }