dongwei2882 2016-04-18 17:27
浏览 41

我应该在生产代码中使用fmt吗?

I see a lot of Go code that looks like this:

func main() {
    response, _, err := http.Get("http://golang.org/")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    }

    defer response.Body.Close()
    contents, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    }

    fmt.Printf("%s
", string(contents))
}

My questions are:

  1. In production, should I keep these fmt.Printf statements? Silly question I am sure but just checking

  2. What logging options do you recommend for production code and also dev?

  • 写回答

4条回答 默认 最新

  • doutuobao4004 2016-04-18 17:36
    关注

    Thought it's acceptable to use printfs in some situation. It's better, in production environment to use different logging strategies.

    For instance if your application is a daemon running in background you are going to miss all of your fmt.Printf unless you don't pipe them in a file.

    A good way to do looging is to use log rotation (since you don't want to have incredibly huge files) instead of reinvent your own logrotate you can use standard packages or external packages based on your needs.

    Golang provides a log package syslog subpackage that are optimal solution to do simple logging relying on the OS syslog.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号