When using Monolog with StreamHandler
, it is normal situation that several instances of PHP script would be writing to the same log file in parallel.
(eg. in my Symfony application, when mulitple users open "Login page" at the same time, it would result in multiple instances of my app script (app.php
) running and thus two instances of Monolog StreamHandler
would be writing into same app/logs/prod.log
.)
How come that despite of concurrent writing to the file, each log lines is not broken in the middle and messed up? How come the situation below never happens:
- instance1 of StreamHanler only wrote half of the log line,
- instance2 of StreamHanler wrote it's first half
- instance1 wrote second half of the log line
- instance2 wrote second half of the log line
- now our log file is a mess, because two lines are mixed.
I tried looking at the source code of the StreamHanler
https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/StreamHandler.php#L93
But I do not see any proper concurrency control, there is a flock()
but it seems to be disabled (->useLocking=false
) in default (Symfony) configuration and it seems that logs are fine without it...
if ($this->useLocking) {
// ignoring errors here, there's not much we can do about them
flock($this->stream, LOCK_EX);
}
fwrite($this->stream, (string) $record['formatted']);
if ($this->useLocking) {
flock($this->stream, LOCK_UN);
}
But why are the logs magically fine?