Apache lets you log a specific apache sub process environment variable in access log. PHP lets you set apache sub process env variable using apache_setenv. Here are the steps log custom data in apache access log when you are using Apache and PHP.
Log custom apache env variable in access log
Here is the syntax for logging env variable named mydata in access log:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{mydata}e\"" combined
Setting custom env variable from PHP
You can use the following code to set key mydata
in apache subprocess env variable
<?php apache_setenv("mydata", "somevalue1"); ?>
In case you need to append something to custom data
mydata
, you can use<?php $current = apache_getenv("mydata"); $newval = $current."somevalue2"; apache_setenv("mydata", $newval); ?>
Possible use cases
Logging data in apache access log makes it easy to grep things from command line. It is easier to analyze apache access logs as everything is in one line. You can probably take a mixed strategy where you log verbose information in separate log files, and important and short information in apache access logs.