I'm kinda new to PHP so I'm wondering if you could explain something to me.
I'm trying to write a class that will append to a log file. I've written the class so that the resource itself is static within the Logger class. This works great for me when I am testing since I can have multiple classes instantiate a Logger object but all will share the same static resource to write to the file.
That said, I now face the question of multiple instances of PHP (multiple users loading my page) and more specifically: concurrency.
Questions:
when my site experiences concurrent php instances that utilize the logger, will they be fighting for access to this file?
I'm on UBUNTU, but will this make a difference in Windows?
In my logger file I do a check every X number of added lines to see what the size of the file is, if size is larger than Y, I close this file and 'rotate' the files (mylog.log2 becomes mylog.log3, mylog.log1 becomes mylog.log2 , mylog.log becomes mylog.log1) and create a brand new mylog.log file. Will this be of concern if multiple instances are writing to it? if so, how can this be handled properly?
Sorry for the newb question...
As long as you have your logger open the file in append mode (e.g. fopen("my.log", "a")
or similar), everyone should be able to write to the file without loss of data (the OS will take care of making sure everything gets added without overwriting). However, the order of writes to the file is not guaranteed across instances - e.g. you will likely get interspersed log entries.
For example, if instance A writes lines a
, b
, c
in that order, and instance B writes lines 1
, 2
, and 3
, you might get the following result:
1
a
b
2
c
3
or any other interleaving.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With