How to log messages to the console and a file both in golang?

前端 未结 1 710
故里飘歌
故里飘歌 2020-12-29 06:15

I can direct all messages to log.txt file:

logFile, err := os.OpenFile(\"log.txt\", os.O_CREATE | os.O_APPEND | os.O_RDWR, 0666)
if err != nil {
    panic(er         


        
相关标签:
1条回答
  • 2020-12-29 06:49

    Use an io.MultiWriter

    MultiWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command

    mw := io.MultiWriter(os.Stdout, logFile)
    log.SetOutput(mw)
    
    0 讨论(0)
提交回复
热议问题