code_junkie

How can I parse Apache's error log in PHP?

Nov 11th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. $contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES);
  2. if (is_array($contents)) {
  3. echo end($contents);
  4. }
  5. unset($contents);
  6.  
  7. php_flag error_log /web/mysite/logs/error.log
  8.  
  9. <?php
  10. exec('tail /usr/local/apache/logs/error_log', $output);
  11. ?>
  12. <Table border="1">
  13. <tr>
  14. <th>Date</th>
  15. <th>Type</th>
  16. <th>Client</th>
  17. <th>Message</th>
  18. </tr>
  19. <?
  20. foreach($output as $line) {
  21. // sample line: [Wed Oct 01 15:07:23 2008] [error] [client 76.246.51.127] PHP 99. Debugger->handleError() /home/gsmcms/public_html/central/cake/libs/debugger.php:0
  22. preg_match('~^[(.*?)]~', $line, $date);
  23. if(empty($date[1])) {
  24. continue;
  25. }
  26. preg_match('~] [([a-z]*?)] [~', $line, $type);
  27. preg_match('~] [client ([0-9.]*)]~', $line, $client);
  28. preg_match('~] (.*)$~', $line, $message);
  29. ?>
  30. <tr>
  31. <td><?=$date[1]?></td>
  32. <td><?=$type[1]?></td>
  33. <td><?=$client[1]?></td>
  34. <td><?=$message[1]?></td>
  35. </tr>
  36. <?
  37. }
  38. ?>
  39. </table>
  40.  
  41. BigFile.php
  42. <?php
  43. $run_test = true;
  44. $test_file = 'BigFile.php';
  45.  
  46. class BigFile
  47. {
  48. private $file_handle;
  49.  
  50. /**
  51. *
  52. * Load the file from a filepath
  53. * @param string $path_to_file
  54. * @throws Exception if path cannot be read from
  55. */
  56. public function __construct( $path_to_log )
  57. {
  58. if( is_readable($path_to_log) )
  59. {
  60. $this->file_handle = fopen( $path_to_log, 'r');
  61. }
  62. else
  63. {
  64. throw new Exception("The file path to the file is not valid");
  65. }
  66. }
  67.  
  68. /**
  69. *
  70. * 'Finish your breakfast' - Jay Z's homme Strict
  71. */
  72. public function __destruct()
  73. {
  74. fclose($this->file_handle);
  75. }
  76.  
  77. /**
  78. *
  79. * Returns a number of characters from the end of a file w/o loading the entire file into memory
  80. * @param integer $number_of_characters_to_get
  81. * @return string $characters
  82. */
  83. public function getFromEnd( $number_of_characters_to_get )
  84. {
  85. $offset = -1*$number_of_characters_to_get;
  86. $text = "";
  87.  
  88. fseek( $this->file_handle, $offset , SEEK_END);
  89.  
  90. while(!feof($this->file_handle))
  91. {
  92. $text .= fgets($this->file_handle);
  93. }
  94.  
  95. return $text;
  96. }
  97. }
  98.  
  99. if( $run_test )
  100. {
  101. $number_of_characters_to_get = 100000;
  102. $bf = new BigFile($test_file);
  103. $text = $bf->getFromEnd( $number_of_characters_to_get );
  104. echo "$test_file has the following $number_of_characters_to_get characters at the end:
  105. <br/> <pre>$text</pre>";
  106. }
  107.  
  108. ?>
Add Comment
Please, Sign In to add comment