Fluentd + golang 日志记录应用程序出错
Fluentd + golang Logging application is giving error
我是 golang 的新手并且很流利。我正在尝试使用来自 github.com/fluent/fluent-logger-golang/fluent .
的 fluentd 记录器库构建日志记录应用程序
在 192.168.0.106 上,fluentd 服务是 运行 并在 24224
上侦听
下面是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)
}
}
执行二进制文件后出现很多错误。
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$
请帮助我解决问题。
谢谢
错误是由这一行引起的:
error := logger.Post(tag,data)
data
是Student
类型,是你自己的struct类型。它包含未导出的字段(名称以小写字母开头的字段)。只能从定义类型的包中访问未导出的字段。
所以当 fluent
包试图访问它们时(通过反射),你会得到运行时恐慌,因为这是不允许的(fluent
包不是你的 main
包) .
解决方法很简单:导出Student
类型的字段:
type Student struct {
Name string
IdNo int
Age int
}
另一种选择是使用 map
,例如:
data := map[string]interface{}{
"name": "John",
"id_no": 1234,
"age": 25,
}
struct
解决方案更灵活,您可以 "remap" 或使用结构标签更改字段在日志中的显示方式,例如:
type Student struct {
Name string `msg:"name"`
IdNo int `msg:"id_no"`
Age int `msg:"age"`
}
阅读此问题+答案以了解有关结构标签的更多信息:
What are the use(s) for tags in Go?
中也有介绍
struct Student
的所有属性都需要导出。把第一个字符改成大写就可以了
type Student struct {
Name string
Id_no int
Age int
}
我是 golang 的新手并且很流利。我正在尝试使用来自 github.com/fluent/fluent-logger-golang/fluent .
的 fluentd 记录器库构建日志记录应用程序在 192.168.0.106 上,fluentd 服务是 运行 并在 24224
上侦听下面是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)
}
}
执行二进制文件后出现很多错误。
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$
请帮助我解决问题。
谢谢
错误是由这一行引起的:
error := logger.Post(tag,data)
data
是Student
类型,是你自己的struct类型。它包含未导出的字段(名称以小写字母开头的字段)。只能从定义类型的包中访问未导出的字段。
所以当 fluent
包试图访问它们时(通过反射),你会得到运行时恐慌,因为这是不允许的(fluent
包不是你的 main
包) .
解决方法很简单:导出Student
类型的字段:
type Student struct {
Name string
IdNo int
Age int
}
另一种选择是使用 map
,例如:
data := map[string]interface{}{
"name": "John",
"id_no": 1234,
"age": 25,
}
struct
解决方案更灵活,您可以 "remap" 或使用结构标签更改字段在日志中的显示方式,例如:
type Student struct {
Name string `msg:"name"`
IdNo int `msg:"id_no"`
Age int `msg:"age"`
}
阅读此问题+答案以了解有关结构标签的更多信息:
What are the use(s) for tags in Go?
中也有介绍struct Student
的所有属性都需要导出。把第一个字符改成大写就可以了
type Student struct {
Name string
Id_no int
Age int
}