log rotation problem on custom application

103 Views Asked by At

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:

  • Can this be fixed from OS side somehow (I am running on Linux), without restarting the application
  • if it can not be fixed from the OS, can I fix this from the application? I can close and open stderr, but I do not really know the filename where stderr points to, since it is redirected. C solution is OK too.

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.

3

There are 3 best solutions below

2
Sam Varshavchik On BEST ANSWER

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.

1
Botje On

Truncating the file by itself does not change the file offset of your stderr file descriptor.

From the application you can call:

ftruncate(2, 0);
lseek(2, 0, SEEK_SET);
1
Yuvraj Singh On

You can also explore logrotate utility.

So, you can use append (2>>) to keep on writing in the errors in log file. Then you can write a logrotate config something like this to rotate the log files:-

$ cat /etc/logrotate.d/log_file
<<path_of_log_file>>/log_file
{
  copytruncate      # To truncate original log file and make a copy of it.
  rotate 10         # To keep only last 10 files and remove older.
  daily             # To rotate the files daily
  size 10M          # Or Rotate when size reaches 10MB
  .....
}

There could be lot of different combinations in logrotate config.

Regards, Yuvraj