random1 Posted September 28, 2009 Share Posted September 28, 2009 Hey All, I'd like to get some help on getting the content of Apache's log files (e.g. apache\logs\error.log). I'd like to get the last 20 entries/lines in the file to avoid loading a giant log file. Any ideas? Best would be an array of each line split into type. Quote Link to comment https://forums.phpfreaks.com/topic/175753-solved-getting-apaches-log-file-data-using-php/ Share on other sites More sharing options...
trq Posted September 28, 2009 Share Posted September 28, 2009 By default the Apache user doesn't have permissions to read these files, only root does. You would need to setup sudo to allow access (see man sudoers), from there, I'd simply use the head command via exec. Quote Link to comment https://forums.phpfreaks.com/topic/175753-solved-getting-apaches-log-file-data-using-php/#findComment-926181 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 I was able to do it this way: <?php $file = '../../Apache/logs/error.log'; //Change to your path $fp = fopen($file, 'r'); fseek($fp, filesize($file)-2500); $content = array_slice(array_reverse(explode("\n",fread($fp, 2500))), 0, 21); array_shift($content); echo "<pre>"; print_r($content); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/175753-solved-getting-apaches-log-file-data-using-php/#findComment-926185 Share on other sites More sharing options...
random1 Posted September 28, 2009 Author Share Posted September 28, 2009 Thanks AlexWD! :D I ended up using: $file = 'D:\\xampp\\apache\\logs\\error.log'; // Path of Apache log file $fp = fopen($file, 'r'); fseek($fp, filesize($file)-5000); $content = array_slice(array_reverse(explode("\n",fread($fp, 5000))), 0, 21); array_shift($content); echo '<pre>'; print_r($content); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/175753-solved-getting-apaches-log-file-data-using-php/#findComment-926203 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Np, just press the "topic Solved" button on the bottom left. Quote Link to comment https://forums.phpfreaks.com/topic/175753-solved-getting-apaches-log-file-data-using-php/#findComment-926205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.