Writing to existing file

97 Views Asked by At

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.

1

There are 1 best solutions below

2
Roman Kiselenko On BEST ANSWER

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