natefinch / lumberjack

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

add O_APPEND flag when open new log file #86

Open crazycs520 opened 5 years ago

crazycs520 commented 5 years ago

What problem does this PR solve?

Fix the problem of truncating file when opening a new log file.

Related: https://superuser.com/questions/881777/what-happens-when-i-truncate-a-file-that-is-in-use

When I started a program with lumberjack, a new log file was created. Then I truncated the log file in another process, but I saw many characters like ^@ in the log file when I opened the file using Vim.

What is changed and how it works?

Add the O_APPEND flag when opening a new log file.

s7v7nislands commented 3 years ago

We have also encountered a similar problem. When our service process is reloaded, two processes will write the same file at the same time. At this time, the log will be overwritten.

@natefinch Can you consider accepting this?

natefinch commented 3 years ago

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.

s7v7nislands commented 3 years ago

yes, we should't use two process to write the same file.

but I have a question, why these two OpenFile in the lumberjack.go have different flag? I think them should both have os.O_APPEND f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode) file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)

Thanks!

s7v7nislands commented 3 years ago

@natefinch