Use the UNIX logrotate utility to manage the regular log file.
If you are running your Perforce server on a UNIX/Linux system then it's likely that logrotate is already installed and being used to manage some of your system log files. As each p4d instance logs to the Perforce server log file directly, rather than through the syslog facility, it's quite easy to configure logrotate to seamlessly rotate the p4d log without having to stop, signal or notify any p4d processes.
Simply Rename the Log File for Instant Log Rotation
When one Perforce server instance needs to append lines to the Perforce server log it makes operating system calls as follows. These file access semantics facilitate orderly logging by multiple p4d instances to the one common log file, and also allow instant file renaming:
- Opens the log file for READ/WRITE, APPEND and will create the log file with mode 0666 if it's missing.
- Lock the log file EXCLUSIVELY.
- Append lines to the log file.
- Unlock the log file.
- Close the log file.
A file rename on UNIX type systems is a simple inode update and so happens almost instantly without the need for file copy and truncate operations. It can even be done while the file is actually being written as the rename doesn't break the file handle being used by a p4d instance writing to the file, so writing can continue. If another p4d process finds the log file does not exist then is simply creates a new file as part of the file open step.
On Windows operating systems a file rename involves copying the original file to the new location and then truncating the original file. This doesn't happen instantly as it can take a while to copy and truncate a file. While the file rename is in progress access to the file is denied.
Sample logrotate Configuration File Stanza to Rotate a p4d Log File
Create the file "/etc/logrotate.d/perforce" with the following content customized to your site specific needs.
# Rotate the Perforce server, p4d, log file
# Choose rotation frequency and criteria to suit your site needs.
# rotate N
# daily, weekly, monthly
# size, maxsize, minsize
# Here we rotate once a week and keep 8 weeks of log files
# Rename/move the log rather than copying because copying it's
# necessary and takes longer.
# Let a p4d instance create a new log as p4d always opens the log
# with a CREATE option and it's better that logrotate not interfere
# Old versions of the log are compressed with gzip by default. You
# probably want to do this to save disk space but ensure you also
# use the delaycompress option. See below. Typically, Perforce server
# logs compress to 10% of their original size.
# Postpone compression of the previous log to the next rotation
# cycle because there may be p4d instances that still have the
# freshly rotated log open. Take care if you are rotating logs
# very frequently as you don't what to be trying to delete logs
# that p4d still has open.
Usually logrotate is run as a daily cron job so adding the above file should be all that you need do to configure logrotate to rotate your p4d log file. To ask logrotate to parse your config file without rotating any files run:
sudo logrotate -d /etc/logrotate.d/perforce