Skip to content

How to use the Apache error log for debug

Apache server logo

Part of your job as a programmer is to track bugs, find errors, or just check some backend results.

Some people find it hard to use the debuggers or they can’t be installed remotely. So you can choose this small cheat to do this job.

This can be done by manipulating and watching the log files of your Web server.

In the Apache web server, you can configure your own error log files. You can set different error logs for your different virtual hosts

Example how to set access and error logs in Apache
Example how to set access and error logs in Apache

The simplest command to watch it in real-time is by typing in the console:

tail -f /var/log/apache2/error.log

(This is the default Apache logs path)

Extended version

To use the extended version you have to add this row in your code:

error_log(print_r('<track_word> '$var_to_print.json_encode(, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),1));

You must set the same ‘track_word’ in both places.

We use json_encode because we have to convert all the objects or arrays to log-compatible look

tail -f /var/log/apache2/wherror.log | grep --line-buffered <track_word> | sed 's/\\n/\n/g'

Paste the code above in the console and run your PHP code. Enjoy

Views: 58

Leave a Reply

Your email address will not be published. Required fields are marked *