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

1
  • From the Apache Documentation -> Access log: "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 Commented Jan 2, 2019 at 14:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Just to be clear, do you replace {hostname} with the hostname or is it a variable that is automatically replaced.
Yes manually replace it with your hostname or use an ENV variable with the name if you have one
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?
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.