dtyyrt4545 2016-04-26 08:12
浏览 424
已采纳

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 08: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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?