How to log information about cookies

apache-2.2

I would like to add cookie information to my Apache access logs ie: email, username (the cookie is created by php file, not Apache).

Can I add something to log.conf file similar to: \"%{cookieName}i\" to

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

If that doesn't work, do I use cookie log and combine it with access log and how?

Best Answer

There are three ways to get cookies into Apache logs:

%{cookiename}C
This will match the named cookie sent in the request by the browser but not any other cookies.

%{Cookie}i
This will log the entire Cookie: header sent to the server in the request. This can be quite extensive if many cookies are set or if any of the cookies have a lot of data.

%{Set-Cookie}o
This will match the entire Set-Cookie: header sent by the server in the response.

Note that "cookiename" should be replaced with the name of the cookie you want to log whereas the other two methods should be placed in your CustomLog directive exactly as written. These two are matching headers in the request and the response respectively. They can be used to match any header, not just the cookie headers.