douzhi6365 2015-11-18 13:18
浏览 158
已采纳

如何在日志中隐藏日志级别和时间

I am trying to write the logs in CSV format. I am trying to use logrus to write the CSV as logs. But I see the log level (info) and the time getting added to the CSV content in logs. Like,

time="2015-11-18T01:27:38-04:00" level=info msg="100,Click,Android"

How can I write only my message using logrus? Or should I use any other loggers available in golang?

Note: I am a newbie to go

  • 写回答

1条回答 默认 最新

  • doulv8162 2015-11-19 14:22
    关注

    I am trying to use logrus to write the CSV as logs. But I see the log level (info) and the time getting added to the CSV content in logs.

    It looks like you definitely shouldn't be using logrus, as the main reason it exists is creating structured logs, i.e. key-value style. Not sure why don't you simply do

    fmt.Println("100,Click,Android")
    

    or

    fmt.Fprintln(os.Stderr, "100,Click,Android")
    

    if you want it to be written into the standard error (default for loggers).

    If for any reason you need a logger, you can pretty much do the same thing with the standard logger by disabling flags:

    log.SetFlags(0)
    log.Println("100,Click,Android")
    
    // Output:
    // 100,Click,Android
    

    However, if you're creating a CSV output yourself (i.e. you have spearate fields rather than full CSV lines already), use csv package from the standard library:

    w := csv.NewWriter(os.Stdout)
    w.Write([]string{"100", "Click", "Android"})
    w.Flush()
    
    // Output:
    // 100,Click,Android
    

    Unless you just want to use logrus, then follow the concept of the package and stick with structured output:

    logrus.WithFields(logrus.Fields{
        "id": "100",
        "event": "Click",
        "os": "Android",
    }).Info("Hooray, we have some users!")
    
    // Output:
    // time="2015-11-19T15:20:32+01:00" level=info msg="Hooray, we have some users!" event=Click id=100 os=Android
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器