Skip to content

Commit 79cb10d

Browse files
authored
Merge pull request #133 from thaJeztah/gofmt
all: gofmt code for current go versions, and fix example in README
2 parents 38df75e + 58f4c93 commit 79cb10d

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

README.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ fluent-logger-golang
88

99
## How to install
1010

11-
```
12-
go get github.com/fluent/fluent-logger-golang/fluent
11+
```bash
12+
go get github.com/fluent/fluent-logger-golang/fluent@latest
1313
```
1414

1515
## Usage
1616

1717
Install the package with `go get` and use `import` to include it in your project.
1818

19-
```
19+
```go
2020
import "github.com/fluent/fluent-logger-golang/fluent"
2121
```
2222

@@ -26,27 +26,32 @@ import "github.com/fluent/fluent-logger-golang/fluent"
2626
package main
2727

2828
import (
29-
"github.com/fluent/fluent-logger-golang/fluent"
30-
"fmt"
31-
//"time"
29+
"fmt"
30+
"time"
31+
32+
"github.com/fluent/fluent-logger-golang/fluent"
3233
)
3334

3435
func main() {
35-
logger, err := fluent.New(fluent.Config{})
36-
if err != nil {
37-
fmt.Println(err)
38-
}
39-
defer logger.Close()
40-
tag := "myapp.access"
41-
var data = map[string]string{
42-
"foo": "bar",
43-
"hoge": "hoge",
44-
}
45-
error := logger.Post(tag, data)
46-
// error := logger.PostWithTime(tag, time.Now(), data)
47-
if error != nil {
48-
panic(error)
49-
}
36+
logger, err := fluent.New(fluent.Config{})
37+
if err != nil {
38+
fmt.Println(err)
39+
}
40+
defer logger.Close()
41+
tag := "myapp.access"
42+
data := map[string]string{
43+
"foo": "bar",
44+
"hoge": "hoge",
45+
}
46+
err = logger.Post(tag, data)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
err = logger.PostWithTime(tag, time.Now(), data)
52+
if err != nil {
53+
panic(err)
54+
}
5055
}
5156
```
5257

@@ -181,7 +186,7 @@ were involved. Starting v1.8.0, the logger no longer accepts `Fluent.Post()`
181186
after `Fluent.Close()`, and instead returns a "Logger already closed" error.
182187

183188
## Tests
184-
```
185189

190+
```bash
186191
go test
187192
```

_examples/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

8-
"../fluent"
8+
"github.com/fluent/fluent-logger-golang/fluent"
99
)
1010

1111
func main() {
@@ -15,9 +15,10 @@ func main() {
1515
}
1616
defer logger.Close()
1717
tag := "myapp.access"
18-
var data = map[string]string{
18+
data := map[string]string{
1919
"foo": "bar",
20-
"hoge": "hoge"}
20+
"hoge": "hoge",
21+
}
2122
for i := 0; i < 100; i++ {
2223
e := logger.Post(tag, data)
2324
if e != nil {

fluent/fluent.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package fluent
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/tls"
7+
"encoding/base64"
8+
"encoding/binary"
69
"encoding/json"
710
"errors"
811
"fmt"
@@ -16,10 +19,6 @@ import (
1619
"sync/atomic"
1720
"time"
1821

19-
"bytes"
20-
"encoding/base64"
21-
"encoding/binary"
22-
2322
"github.com/tinylib/msgp/msgp"
2423
)
2524

@@ -323,7 +322,7 @@ func (chunk *MessageChunk) MarshalJSON() ([]byte, error) {
323322
if err != nil {
324323
return nil, err
325324
}
326-
return []byte(fmt.Sprintf("[\"%s\",%d,%s,%s]", chunk.message.Tag,
325+
return []byte(fmt.Sprintf(`["%s",%d,%s,%s]`, chunk.message.Tag,
327326
chunk.message.Time, data, option)), err
328327
}
329328

fluent/fluent_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ func newTestDialer() *testDialer {
105105
// conn := d.waitForNextDialing(true, false)
106106
// assertReceived(t, // t is *testing.T
107107
// conn.waitForNextWrite(true, ""),
108-
// "[\"tag_name\",1482493046,{\"foo\":\"bar\"},{}]")
108+
// `["tag_name",1482493046,{"foo":"bar"},{}]`)
109109
//
110110
// f.EncodeAndPostData("something_else", time.Unix(1482493050, 0), map[string]string{"bar": "baz"})
111111
// assertReceived(t, // t is *testing.T
112112
// conn.waitForNextWrite(true, ""),
113-
// "[\"something_else\",1482493050,{\"bar\":\"baz\"},{}]")
113+
// `["something_else",1482493050,{"bar":"baz"},{}]`)
114114
//
115115
// In this example, the 1st connection dialing succeeds but the 1st attempt to write the
116116
// message is discarded. As the logger discards the connection whenever a message
@@ -120,7 +120,7 @@ func newTestDialer() *testDialer {
120120
// using assertReceived() to make sure the logger encodes the messages properly.
121121
//
122122
// Again, the example above is using async mode thus, calls to f and conn are running in
123-
// the same goroutine. However in sync mode, all calls to f.EncodeAndPostData() as well
123+
// the same goroutine. However, in sync mode, all calls to f.EncodeAndPostData() as well
124124
// as the logger initialization shall be placed in a separate goroutine or the code
125125
// allowing the dialing and writing attempts (eg. waitForNextDialing() & waitForNextWrite())
126126
// would never be reached.
@@ -311,7 +311,8 @@ func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) {
311311

312312
f, err := New(Config{
313313
FluentNetwork: network,
314-
FluentSocketPath: socketFile})
314+
FluentSocketPath: socketFile,
315+
})
315316
if err != nil {
316317
t.Error(err)
317318
return
@@ -324,7 +325,8 @@ func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) {
324325
network = "unixxxx"
325326
fUnknown, err := New(Config{
326327
FluentNetwork: network,
327-
FluentSocketPath: socketFile})
328+
FluentSocketPath: socketFile,
329+
})
328330
if _, ok := err.(*ErrUnknownNetwork); !ok {
329331
t.Errorf("err type: %T", err)
330332
}
@@ -350,12 +352,12 @@ func Test_MarshalAsMsgpack(t *testing.T) {
350352
f := &Fluent{Config: Config{}}
351353

352354
tag := "tag"
353-
var data = map[string]string{
355+
data := map[string]string{
354356
"foo": "bar",
355-
"hoge": "hoge"}
357+
"hoge": "hoge",
358+
}
356359
tm := time.Unix(1267867237, 0)
357360
result, err := f.EncodeData(tag, tm, data)
358-
359361
if err != nil {
360362
t.Error(err)
361363
}
@@ -383,7 +385,6 @@ func Test_SubSecondPrecision(t *testing.T) {
383385
encodedData, err := fluent.EncodeData("tag", timestamp, map[string]string{
384386
"foo": "bar",
385387
})
386-
387388
// Assert no encoding errors and that the timestamp has been encoded into
388389
// the message as expected.
389390
if err != nil {
@@ -402,12 +403,12 @@ func Test_SubSecondPrecision(t *testing.T) {
402403
func Test_MarshalAsJSON(t *testing.T) {
403404
f := &Fluent{Config: Config{MarshalAsJSON: true}}
404405

405-
var data = map[string]string{
406+
data := map[string]string{
406407
"foo": "bar",
407-
"hoge": "hoge"}
408+
"hoge": "hoge",
409+
}
408410
tm := time.Unix(1267867237, 0)
409411
result, err := f.EncodeData("tag", tm, data)
410-
411412
if err != nil {
412413
t.Error(err)
413414
}
@@ -494,14 +495,14 @@ func TestPostWithTime(t *testing.T) {
494495
conn := d.waitForNextDialing(true, false)
495496
assertReceived(t,
496497
conn.waitForNextWrite(true, ""),
497-
"[\"acme.tag_name\",1482493046,{\"foo\":\"bar\"},{}]")
498+
`["acme.tag_name",1482493046,{"foo":"bar"},{}]`)
498499

499500
assertReceived(t,
500501
conn.waitForNextWrite(true, ""),
501-
"[\"acme.tag_name\",1482493050,{\"fluentd\":\"is awesome\"},{}]")
502+
`["acme.tag_name",1482493050,{"fluentd":"is awesome"},{}]`)
502503
assertReceived(t,
503504
conn.waitForNextWrite(true, ""),
504-
"[\"acme.tag_name\",1634263200,{\"welcome\":\"to use\"},{}]")
505+
`["acme.tag_name",1634263200,{"welcome":"to use"},{}]`)
505506
})
506507
}
507508
}
@@ -545,7 +546,7 @@ func TestReconnectAndResendAfterTransientFailure(t *testing.T) {
545546
conn := d.waitForNextDialing(true, false)
546547
assertReceived(t,
547548
conn.waitForNextWrite(true, ""),
548-
"[\"tag_name\",1482493046,{\"foo\":\"bar\"},{}]")
549+
`["tag_name",1482493046,{"foo":"bar"},{}]`)
549550

550551
// The next write will fail and the next connection dialing will be dropped
551552
// to test if the logger is reconnecting as expected.
@@ -556,7 +557,7 @@ func TestReconnectAndResendAfterTransientFailure(t *testing.T) {
556557
conn = d.waitForNextDialing(true, false)
557558
assertReceived(t,
558559
conn.waitForNextWrite(true, ""),
559-
"[\"tag_name\",1482493050,{\"fluentd\":\"is awesome\"},{}]")
560+
`["tag_name",1482493050,{"fluentd":"is awesome"},{}]`)
560561
})
561562
}
562563
}

0 commit comments

Comments
 (0)