Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it needed to add 'O_APPEND' flag here? #148

Open
ultraji opened this issue Dec 22, 2021 · 2 comments
Open

Is it needed to add 'O_APPEND' flag here? #148

ultraji opened this issue Dec 22, 2021 · 2 comments

Comments

@ultraji
Copy link

ultraji commented Dec 22, 2021

f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)

My scene(kube-apiserver audit.log):

  1. use logrotate at my workplace to handle log rotation (size > 50M then rotate, copyrtuncate) and make audit-log-maxsize very large (--audit-log-maxsize=100000) so native rotation will not start.
  2. if there is no audit.log file before, it will be open by flag 'os.O_CREATE|os.O_WRONLY|os.O_TRUNC',so when logrotate to rotate the log file, i'm getting binary thrash in log file, and audit.log keeping growing.
  3. if there is an exsiting audit.log file before, it will be open by flag 'os.O_APPEND|os.O_WRONLY', so when logrotate to rotate the log file, copytruncate works well, and audit.log is truncated as we hope.

In my test:

I add O_APPEND flag here, everthing work well, and audit.log is truncated as we hope. so is it needed to add 'O_APPEND' flag here?

@natefinch
Copy link
Owner

Truncate and append don't make any sense together. Truncate truncates, there's nothing to append to.

You said "when logrotate to rotate the log file" ... if lumberjack is writing to the file, you can't have logrotate rotating the file. Lumberjack won't know the file has changed, and will continue writing at the place on disk it last knew to be the end of the file. That's probably the problem. You can't have any other application changing the log file. Only lumberjack can change it.

@ultraji
Copy link
Author

ultraji commented Dec 22, 2021

I agree that "You can't have any other application changing the log file" .But in my opinion, with O_APPEND flag, "The file is opened in append mode. Before each write(2), the file offset is positioned at the end of the file, as if with lseek(2)", so Lumberjack can know when the file is truncated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants