c++clinuxstderr

log rotation problem on custom application


I have server program, written in C++. I usually start it as

db_net 2> log_file

I do not use anything special about logging, just print on stderr.

However sometimes, the log_file become huge. If I do

echo > log_file

It does not have effect, the file is not shrieked to zero. I know this worked 10 years ago, but now it does not.

Question is following:

I know MySQL doing this with flush logs; and apache / nginx can do the same, however they know the filename and in their case is easy to reopen.

I know I can implement log file inside configuration, but currently I am looking for fast solution.

Filesystem is XFS.


Solution

  • Use append mode:

    db_net 2>> log_file
    

    Now when you manually truncate the file, the process will just continue to append to the new, empty file.