5

Read concurrency of flat files is almost unlimited (correct me if I'm wrong); but how is the concurrency for write? Consider a simple access log writing (for visits) in PHP as to append a line of access detail ended with \n

fopen(); // in append mode
fwrite();
fclose();

Since we have concurrent visitors, how the system (one user which is the wbserver/php user) write the view logs concurrently?

My confusion is: the function file_put_contents() (which is a wrapper of the above three functions) has an option for locking (LOCK_EX)? Is it beneficial to use this locking option? How this will affect the log writing?

UPDATE: My question is about how LOCKing originally works/affects writing log to file. I do not compare file_put_contents and fwrite; even my question is not limited to PHP. The question is about locking a file during write process.

Googlebot
  • 15,159
  • 44
  • 133
  • 229
  • `file_put_contents()` does have an option for locking, check the [manual](http://php.net/manual/en/function.file-put-contents.php). – yannis May 29 '12 at 03:04
  • @YannisRizos As I quoted in the original question, I know that `file_put_contents` has `LOCK_EX`. My question is how it affects writing an access log. – Googlebot May 29 '12 at 03:13
  • Yes, you're right, obviously my comment was for the second revision of the question. Bad edit, that one. – yannis May 29 '12 at 03:17

2 Answers2

3

file_put_contents() does have an option for locking, but in my opinion, you should use flock() instead.

From manual:

Available flags

FILE_USE_INCLUDE_PATH
Search for filename in the include directory. See include_path for more information.

FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it.

LOCK_EX Acquire an exclusive lock on the file while proceeding to the writing.

flock() allows you to use other methods:

LOCK_SH to acquire a shared lock (reader).
LOCK_EX to acquire an exclusive lock (writer).
LOCK_UN to release a lock (shared or exclusive).

See here: http://php.net/manual/en/function.flock.php and here: http://php.net/manual/en/function.file-put-contents.php

0

Be warned, file locking for access logs is an environment that can easily introduce a race condition. perhaps look into another method? logging to a database, maybe? It's far too easy for a well-designed system to try to open and write to the log at the same time with parallel processes.

Maybe take a look at monolog? there's some logging handlers in there for async logging, which would be ideal for anything to do with access logging.

https://github.com/Seldaek/monolog

damianb
  • 1,224
  • 7
  • 16
  • Nice to know about `monolog`; but this is a custom access log, not that of system. It can contain info from sessions, cookies, member login, etc. In addition, my question is in general for writing to disk. – Googlebot May 29 '12 at 03:39
  • @Ali You need to be very careful then if it's going to be hooked up to anything chatty; the more often you'll be logging data, the higher the chance of having your code go to append to the log only to have it hit the file lock and be unable to log. In this circumstance you need to either be logging to something on an SSD (fast write speed, but unlikely to have as server hardware), a ramdisk (fast write but unstable; lose power and the data is gone), or you can log to a spinny disk and cross your fingers and hope that nothing gets file-locked from writing to the log. – damianb May 29 '12 at 03:50