Skip to content

all: gofmt code for current go versions, and fix example in README #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand All @@ -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)
}
}
```

Expand Down Expand Up @@ -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
```
7 changes: 4 additions & 3 deletions _examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"../fluent"
"github.com/fluent/fluent-logger-golang/fluent"
)

func main() {
Expand All @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions fluent/fluent.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fluent

import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
Expand All @@ -16,10 +19,6 @@ import (
"sync/atomic"
"time"

"bytes"
"encoding/base64"
"encoding/binary"

"github.com/tinylib/msgp/msgp"
)

Expand Down Expand Up @@ -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
}

Expand Down
35 changes: 18 additions & 17 deletions fluent/fluent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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"},{}]`)
})
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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"},{}]`)
})
}
}
Expand Down