首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fluentd + golang测井应用程序出现错误

Fluentd + golang测井应用程序出现错误
EN

Stack Overflow用户
提问于 2016-04-26 08:12:30
回答 2查看 1K关注 0票数 4

我是刚到金刚和流利的新手。我正在尝试使用fluentd的记录器库构建日志应用程序,网址是github.com/fluent/fluent- logger -golang/fluent。

192.168.0.106流畅服务正在运行并收听24224

下面是在戈朗登录的程序。

代码语言:javascript
复制
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)
    }
}

在执行二进制文件后,我得到了很多错误。

代码语言:javascript
复制
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$

请帮助我解决这个问题。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-26 08:15:56

此错误是由以下一行引起的:

代码语言:javascript
复制
error := logger.Post(tag,data)

dataStudent类型,它是您自己的结构类型。它包含未导出的字段(以小写字母开头的字段)。未导出字段只能从定义类型的包中访问。

因此,当fluent包尝试访问它们(通过反射)时,您会遇到运行时的恐慌,因为这是不允许的(fluent包不是您的main包)。

解决方案很简单:导出Student类型的字段:

代码语言:javascript
复制
type Student struct {
    Name string
    IdNo int
    Age int
}

另一种选择是使用map,例如:

代码语言:javascript
复制
data := map[string]interface{}{
    "name": "John",
    "id_no": 1234,
    "age": 25,
}

struct解决方案更灵活,因为您可以用struct标记“重新映射”或更改字段在日志中的显示方式,例如:

代码语言:javascript
复制
type Student struct {
    Name string `msg:"name"`
    IdNo int    `msg:"id_no"`
    Age int     `msg:"age"`
}

有关struct标记的更多信息,请阅读此question+answers:

Go中的标签有什么用途?

它也包含在规范:结构类型中。

票数 3
EN

Stack Overflow用户

发布于 2016-04-26 08:16:34

需要导出struct Student的所有属性。只要把第一个字符改为大写,就行了。

代码语言:javascript
复制
type Student struct {
    Name  string
    Id_no int
    Age   int
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36859042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档