Search code examples
pythonlinuxubuntulogrotate

Null value in the beginning of log after logrotate


I am using logrotate to manage my logs. As I have to manage a bunch of log files. My logrotate config looks like

/log/typeA*.log
/log/typeB*.log
/log/typeC*.log{
        daily
        rotate 7
        copytruncate
        size 1M
        compress
        su root root
        create 0644 root root
        missingok
}

After logrotation is complete I see bunch of null characters in the beginning of file. Which looks like

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@

Looks like infinitely long string. Also file size looks similar. There is solution suggested https://serverfault.com/a/510470 to use postrotate script like

postrotate
   sed -i -e 's/\o00//g' "$1"
endscript

But using this script will modify the file and change stream. So logs won't be redirected to some file stream which doesn't exist.
I am generating logs using python logging module


Solution

  • Same thing happened to me and was able to fix it.

    The reason behind it is that the rotating file is not opened with the flag O_APPEND. Therefore, when rotating it still maintains the same writing offset and starts writing from the middle of the new file. Everything before that offset is marked as NULL, as the beginning of the file is empty.

    In other words, you finish writing character number n in a file and truncate it when the rotation is triggered. Instead of starting again from character number 0, you continue from character number n+1, and the first n characters will be marked as NULL.

    To avoid this issue, you have to open the file with flag O_APPEND.

    Further information can be found in the following thread: https://groups.google.com/g/comp.unix.solaris/c/Zc7ysjMGprQ?pli=1