PHP Logging Fixes

I encountered a problem with an application I was working on the other day that was running on a machine with an older version of PHP (version 4.2.3 I believe). Essentially, the problem was with trying to set the PHP ini variable error_prepend_string. What this variable is supposed to do is the following:

Suppose I have the following blip of code:

For anyone that knows PHP, they will undoubtedly realize that their output will look like the following:

Well, if I had the following blip of code, I would be able to see some more debugging information and/or some cool coloring:

and the output would look like the following:

Now, this makes it easier to debug, and you can do some cool styling to this to make it easier on the eyes. However, in production environments where you don’t want these messages printed to the screen, you generally log it to a file. That’s where PHP’s log_errors and display_errors ini settings come into play. If you set them to 1 and 0, respectively, PHP will log it to the error log set in apache by default. You can also set the ini setting error_log to a file and it will log to that, rather than Apache’s error log (assuming the file has the appropriate write permissions on it).

So, we reach the actual problem with the logging. Somehow, in the version of PHP on the server, the error log only shows the following line for the same code above:

So what happened to my prepended and appended settings? They’re completely toast here. I haven’t had a chance to look at the actual PHP source yet to figure out if this is an ongoing bug, but it’s pretty apparent that we won’t be able to get any debugging information here.

Essentially, I was hoping to log the username prepended here so that I can tail the file (tail -f log_file.txt | egrep '(neraath|chris)') and grep out just my username, since we have multiple developers on this thing, and it’s a pain to see their errors and think they’re yours.

So, what can I possibly do in this situation? Well, this is something I found out yesterday, but you just replace the standard PHP error_logging function with one you write on your own! Now, if you think about this, you might think that that’s a big hoax, because you would have to code the actual source of PHP recompile it from scratch, yada yada yada. Well, no. That’s not the case. You can actually write your own logging function inside PHP and then use a PHP built-in function to tell PHP to use it from now on. Here’s how:

That code will give me the desired result in the log file like such:

Cheers. :-)