dtyyrt4545 2016-04-26 00:12
浏览 425
已采纳

Fluentd + Golang日志记录应用程序出现错误

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

展开全部

  • 写回答

2条回答 默认 最新

  • dongmei1828 2016-04-26 00:15
    关注

    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.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部