natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.8k stars 591 forks source link

Why does openNew not use O_APPEND when open a new logfile? #60

Open bigwhite opened 6 years ago

bigwhite commented 6 years ago

When open existing log file, the openExistingOrNew function use OpenFile with O_APPEND flag, but when creating a new log file, openNew use OpenFile without O_APPEND flag. The two behavior are different. Could anybody tell me why ? In my opinion, openNew should use O_APPEND flag as same as openExistingOrNew does.

The file opened with O_APPEND flag will allow multi-thread/multi-process writing if every write is smaller than 4k. We do not need to use mutex to protect the write.

navygg commented 3 years ago

The author answered in #86: You can't have two processes fiddling with the file at the same time. Lumberjack has to be in charge of the file, because it keeps track of it's size internally. If something else truncates the file or another process writes to it, then it won't work.