How to write log to file

后端 未结 11 1260
抹茶落季
抹茶落季 2020-12-07 08:06

I\'m trying to write to a log file with Go.

I have tried several approaches, all of which have failed. This is what I have tried:

func TestLogging(t          


        
11条回答
  •  攒了一身酷
    2020-12-07 08:39

    I usually print the logs on screen and write into a file as well. Hope this helps someone.

    f, err := os.OpenFile("/tmp/orders.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
    }
    defer f.Close()
    wrt := io.MultiWriter(os.Stdout, f)
    log.SetOutput(wrt)
    log.Println(" Orders API Called")
    

提交回复
热议问题