Search code examples
gologrus

Writing to existing file


here is the code:

tmp, _ := os.OpenFile(filepath.Join(this.dirPath , "Log_"+time.Now().Format(conf.FormatFile())), os.O_CREATE|os.O_WRONLY, os.ModePerm)
logrus.SetOutput(tmp)

it works, but if the program is run again and a file with the same name already exists, writing to it does not occur, there are no errors, logs are not written and that’s it. It seems to me something with this os.ModePerm flag.

The problem is repeated only on linux.


Solution

  • You probably should use os.O_APPEND flag.

    tmp, _ := os.OpenFile(
      filepath.Join(this.dirPath, "Log_"+time.Now().Format(conf.FormatFile())), 
      os.O_APPEND|os.O_WRONLY, 
      os.ModePerm,
    )
    

    https://godoc.org/os#pkg-constants