0

We have a PHP application running on Apache and want to log all API requests (GET + parameters).

I have seen this post Best way to log POST data in Apache? where it says "the GET requests will be easy, because they will be in the apache log".

However, when I look in our logs, they are not there. What server log settings do I need to have to record GET requests + querystring? No mention of how to do this in https://httpd.apache.org/docs/2.4/logs.html

James
  • 1,233
  • 3
  • 13
  • 18
  • From the [Apache Documentation -> Access log](https://httpd.apache.org/docs/2.4/logs.html#accesslog): "The server access log records all requests processed by the server." It explicitly contains an example where a GET request is logged (paragraph 8) Maybe you could compare the settings explained there with yours to see if your server logs incoming requests – akraf Jan 02 '19 at 14:29

1 Answers1

1

The GET request are logged in the access log file. Read the documentation you provided, especially the Access Log part is important. Your Apache host should be configured with something like:

LogLevel        info
ErrorLog        "/private/var/log/apache2/{hostname}-error.log"
CustomLog       "/private/var/log/apache2/{hostname}-access.log" combined

GET requests can then be found in /private/var/log/apache2/{hostname}-access.log

An easy and quick way to do this for debugging purposes is to write a function that logs the POST data.

function logPost() {
    if (isset($_POST && !empty($_POST) {
        foreach($_POST as $key => $value) {
            error_log('=== _POST REQUEST ===');
            error_log('_POST: '.$key.' => '.$value);
        }
        // OR serialise the data but this is less readable
        error_log('=== _POST REQUEST ===');
        error_log(serialise($_POST));
    }
}

POST requests can then be found in /private/var/log/apache2/{hostname}-error.log

G4Hu
  • 338
  • 1
  • 3
  • 18
  • Just to be clear, do you replace {hostname} with the hostname or is it a variable that is automatically replaced. – James Jan 03 '19 at 11:04
  • Yes manually replace it with your hostname or use an ENV variable with the name if you have one – G4Hu Jan 03 '19 at 11:07
  • Just to be clear and I may have to rewrite the question - I am looking to log GET requests made to another server NOT GET requests on this server. Will this still work? – James Jan 03 '19 at 11:09
  • Ah, that would have been good to make more clear in the question. Apache can only log requests made TO it, not from it. At least as far a i know – G4Hu Jan 03 '19 at 11:20
  • If you where to query your own server `http://host.example/index.php?query=test_api` this would be logged by Apache. If you make a request from inside php `header(...)` for example, that will not be logged – G4Hu Jan 03 '19 at 11:24