title: Concurrent appends to the same file using Perl
body: <p>I have a need to upgrade a Perl CGI script where the users must complete 3 steps.  After they finish each step, the script is logging which step the user completed.  Having a record of this is important so we can prove to the user that they only finished step one and didn't complete all three steps, for example.</p>
  
  <p>Right now, the script is creating 1 log file for each instance of the CGI script.  So if UserA does step 1, then UserB does step 1, then step 2, then step 3 - and then UserA finishes step 2 and step 3, the order of the log files would be.</p>
  
  <pre><code>LogFile.UserA.Step1
  LogFile.UserB.Step1
  LogFile.UserB.Step2
  LogFile.UserB.Step3
  LogFile.UserA.Step2
  LogFile.UserA.Step3
  </code></pre>
  
  <p>The log files are named with the current timestamp, a random number, and the process PID.</p>
  
  <p>This works fine to prevent the same file from getting written to more than once, but the directory quickly gets thousands of files (each file contains just a few bytes in it).  There is a process to rotate and compress these logs, but it has fallen upon me to make it so the script logs to just one file a day to reduce the number of log files being created.</p>
  
  <p>Basically, the log file will have the current date in the file name, and anytime the CGI script needs to write to the log, it will append to the one log file for that day, regardless of the user or what step they are on.</p>
  
  <p>Nothing will need to be reading the log file - the only thing that will happen to it is an append by the CGI script.  The log rotation will run on log files that are 7 days or older.  </p>
  
  <p>My question is, what is the best way to handle the concurrent appends to this log file?  Do I need to lock it before appending?  I found <a href="http://www.perlmonks.org/?node_id=486488" rel="noreferrer">this page</a> on Perl Monks that seems to indicate that "when multiple processes are writing to the same file, and all of them have the file opened for appending, data shall not be overwritten."</p>
  
  <p>I've learned that just because it can be done doesn't mean that I should, but in this case, what is the safest, best practice way to do this?</p>
  
  <p>Summary:</p>
  
  <ul>
  <li>Concurrent appends to the same file</li>
  <li>Each append to the file is just one line, less than 50 characters</li>
  <li>Order does not matter</li>
  </ul>
  
  <p>Thanks!</p>