2008-10-01 30 views

Respuesta

13

Hay algunas cosas a tener en cuenta en primer lugar:

  1. En primer lugar, el usuario PHP no pueden tener acceso a los archivos de registro de Apache.
  2. En segundo lugar, PHP y Apache no le dirán dónde está dicho archivo de registro,
  3. Por último, los archivos de registro de Apache pueden llegar a ser bastante grandes.

Sin embargo, si ninguno de estos se aplica, puede utilizar los comandos normales de lectura de archivos para hacerlo. La forma más sencilla de obtener el último error es

$contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES); 
if (is_array($contents)) { 
    echo end($contents); 
} 
unset($contents); 

Probablemente hay una mejor forma de hacerlo que no Oink memoria, pero lo dejo como ejercicio para el lector.

Un último comentario: PHP también tiene una configuración ini para redirigir los errores de PHP a un archivo de registro: error_log = /path/to/error.log

Es posible ajustar esta en httpd.conf o en un archivo .htaccess (si tiene acceso a uno) usando la notación php_flag:

php_flag error_log /web/mysite/logs/error.log 
+0

"Un ejercicio para el lector", ¡cuántas veces he oído eso! ;) – willasaywhat

+2

Bueno, en este caso, significa que estoy en el trabajo y no tengo mucho tiempo para escribir código no relacionado con el trabajo. :) – Powerlord

3

hay montones de scripts PHP que hacen esto, sólo hacer una búsqueda en Google de ejemplos. si quiere hacer su propia versión, no es más complejo que leer cualquier otro archivo. solo asegúrate de saber la ubicación de tus archivos de registro (definidos en el archivo httpd.conf) y el format your log files. El formato también está definido en httpd.conf

10

para cualquier otra persona que esté buscando un script de muestra, arrojé algo juntos, tiene lo básico:

<?php 
exec('tail /usr/local/apache/logs/error_log', $output); 
?> 
<Table border="1"> 
    <tr> 
     <th>Date</th> 
     <th>Type</th> 
     <th>Client</th> 
     <th>Message</th> 
    </tr> 
<? 
    foreach($output as $line) { 
     // 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 
     preg_match('~^\[(.*?)\]~', $line, $date); 
     if(empty($date[1])) { 
      continue; 
     } 
     preg_match('~\] \[([a-z]*?)\] \[~', $line, $type); 
     preg_match('~\] \[client ([0-9\.]*)\]~', $line, $client); 
     preg_match('~\] (.*)$~', $line, $message); 
     ?> 
    <tr> 
     <td><?=$date[1]?></td> 
     <td><?=$type[1]?></td> 
     <td><?=$client[1]?></td> 
     <td><?=$message[1]?></td> 
    </tr> 
     <? 
    } 
?> 
</table> 
0

¿Has probado biterScripting? Soy un administrador de sistemas y he estado usando para analizar registros. Es un script de estilo univx. biterScripting.com -> Descarga gratuita.

3

Aquí hay una clase pequeña que facilita la lectura de una cantidad de caracteres desde la parte posterior de un archivo grande sin memoria de sobrecarga. La configuración de prueba te permite verlo en acción canibalizándose a sí mismo.

BigFile.php 
<?php 
$run_test = true; 
$test_file = 'BigFile.php'; 

class BigFile 
{ 
private $file_handle; 

/** 
* 
* Load the file from a filepath 
* @param string $path_to_file 
* @throws Exception if path cannot be read from 
*/ 
public function __construct($path_to_log) 
{ 
    if(is_readable($path_to_log)) 
    { 
     $this->file_handle = fopen($path_to_log, 'r'); 
    } 
    else 
    { 
     throw new Exception("The file path to the file is not valid"); 
    } 
} 

/** 
* 
* 'Finish your breakfast' - Jay Z's homme Strict 
*/ 
public function __destruct() 
{ 
    fclose($this->file_handle); 
} 

/** 
* 
* Returns a number of characters from the end of a file w/o loading the entire file into memory 
* @param integer $number_of_characters_to_get 
* @return string $characters 
*/ 
public function getFromEnd($number_of_characters_to_get) 
{ 
    $offset = -1*$number_of_characters_to_get; 
    $text = ""; 

    fseek($this->file_handle, $offset , SEEK_END); 

    while(!feof($this->file_handle)) 
    { 
     $text .= fgets($this->file_handle); 
    } 

    return $text; 
} 
} 

if($run_test) 
{ 
$number_of_characters_to_get = 100000; 
$bf = new BigFile($test_file); 
$text = $bf->getFromEnd($number_of_characters_to_get); 
echo "$test_file has the following $number_of_characters_to_get characters at the end: 
    <br/> <pre>$text</pre>"; 
} 

?> 
Cuestiones relacionadas