I am newbie to golang and fluentd. I am trying to build logging application using fluentd's logger library from github.com/fluent/fluent-logger-golang/fluent .
On 192.168.0.106 fluentd service is running and listening on 24224
Below is the program for logging in golang.
package main
import (
"github.com/fluent/fluent-logger-golang/fluent"
"fmt"
)
type Student struct {
name string
id_no int
age int
}
func main() {
logger,err :=fluent.New(fluent.Config{FluentHost:"192.168.0.106", FluentPort:24224})
fmt.Println(logger.BufferLimit)
if err != nil{
fmt.Println("error creating fluentd instance")
return
}
defer logger.Close()
tag := "fluentd-log-demo"
data := Student{"John",1234,25}
error := logger.Post(tag,data)
if error != nil{
panic(error)
}
}
I am getting lots of error after executing the binary file.
mahesh@ubuntu14:~/golang$ fluentd-log-demo
8388608
panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
goroutine 1 [running]:
panic(0x5674e0, 0xc82000a6f0)
/home/mahesh/gotools/src/runtime/panic.go:464 +0x3e6
reflect.valueInterface(0x5674e0, 0xc820010300, 0xb8, 0x1, 0x0, 0x0)
/home/mahesh/gotools/src/reflect/value.go:919 +0xe7
reflect.Value.Interface(0x5674e0, 0xc820010300, 0xb8, 0x0, 0x0)
/home/mahesh/gotools/src/reflect/value.go:908 +0x48
github.com/fluent/fluent-logger-golang/fluent.(*Fluent).PostWithTime(0xc82007a000, 0x603120, 0x10, 0xeceb11576, 0x28abb08c, 0x6d7bc0, 0x5b2060, 0xc820010300, 0x0, 0x0)
/home/mahesh/golib/src/github.com/fluent/fluent-logger-golang/fluent/fluent.go:139 +0x315
github.com/fluent/fluent-logger-golang/fluent.(*Fluent).Post(0xc82007a000, 0x603120, 0x10, 0x5b2060, 0xc820010300, 0x0, 0x0)
/home/mahesh/golib/src/github.com/fluent/fluent-logger-golang/fluent/fluent.go:116 +0x96
main.main()
/home/mahesh/golang/src/GoBasics/fluentd-log-demo/main.go:25 +0x39e
mahesh@ubuntu14:~/golang$
Please help me in troubleshooting the issue.
Thank you
The error is caused by this line:
error := logger.Post(tag,data)
data
is of type Student
, it is your own struct type. It contains unexported fields (fields whose names start with lowercase letters). Unexported fields can only be accessed from the package in which the type is defined.
So when the fluent
package tries to access them (via reflection), you get a runtime panic as this is not allowed (fluent
package is not your main
package).
Solution is simple: export the fields of the Student
type:
type Student struct {
Name string
IdNo int
Age int
}
Another alternative is to use a map
, e.g.:
data := map[string]interface{}{
"name": "John",
"id_no": 1234,
"age": 25,
}
The struct
solution is more flexible though as you can "remap" or change how the fields will appear in the log with struct tags, e.g.:
type Student struct {
Name string `msg:"name"`
IdNo int `msg:"id_no"`
Age int `msg:"age"`
}
Read this question+answers for more on struct tags:
What are the use(s) for tags in Go?
It is also covered in the Spec: Struct types.
All properties of struct Student
need to be exported. Just change the first char to be uppercase, it'l work
type Student struct {
Name string
Id_no int
Age int
}