Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/02/2010, 08:24
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
Respuesta: Tratar los errores php de una pagina con una funcion de error propia

No sé como evitar que me escriba en el fichero de log el texto 2 veces.

¿Qué os parece la función que he encontrado?

Código PHP:
Ver original
  1. <?
  2. /*******************************************************************************
  3. Program Name     : Error.php
  4. Purpose         : This file contains the error class,which manages errors.
  5.                   It handles erros in customized way
  6. Creation Date     : 04/22/2005 02:05PM
  7. ********************************************************************************/
  8. class Error
  9. {
  10.     var $logfile;
  11.     var $debug;
  12.    
  13. /*==============================================================================
  14. Constructor
  15. Arguments :- $debug_yesno. If 1 then errors will be logged,else displayed
  16. ==============================================================================*/
  17.     function Error($debug_yesno=0,$logfile_path)
  18.     {
  19.         $this->debug=$debug_yesno;
  20.         $this->printdebug=1;
  21.  
  22.         //Report only user generated error messages
  23.         error_reporting(E_ALL|E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE);    
  24.        
  25.         if($this->debug>=1)
  26.         {
  27.             $this->logfile=$logfile_path;
  28.             set_error_handler(array($this,"ErrorHandler"));
  29.         }
  30.         else
  31.         {
  32.             restore_error_handler();
  33.         }
  34.     }      
  35.    
  36. /*==============================================================================
  37. Following function logs an error message
  38. ==============================================================================*/
  39.     function ErrorHandler($errno,$errstr,$errfile,$errline)
  40.     {
  41.             $errtime=date("H:i:s - d/m/y");
  42.             $error_message="An error occured on ".$errtime."\n";
  43.             $error_message .="Details are as follows\n";
  44.             $error_message .="Error Number : $errno\n";
  45.             $error_message .=$errstr."\nOccured in ".$errfile."\n";
  46.             $error_message .="On Line $errline \n\n";
  47.             $error_message .=str_repeat("-",50);
  48.             $error_message .="\n";
  49.             $fp=fopen($this->logfile,"a");
  50.             fwrite($fp,$error_message);
  51.             fclose($fp);
  52.     }
  53.  
  54. /*==============================================================================
  55. Following function prints debug messages on the screen
  56. ==============================================================================*/
  57.     function PrintDebug($message,$yesno=0)
  58.     {
  59.         if($yesno>0)
  60.         {
  61.             $message="<font face=\"verdana\" size=\"2\" color=\"red\">Debug : ".$message."</font><br>";
  62.             print $message;
  63.         }
  64.     }
  65. }
  66. ?>
  67.  
  68. <?
  69. //If initialized with 1 errors will be logged. else they will be displayed on the screen
  70. $error=new Error(1,"err.log.txt");
  71.  
  72. $conn=mysql_connect('localhost','root','12345');
  73.  
  74. if(!$conn)
  75. {
  76.     trigger_error("Couldn't Connect To MySQL Because Of Following Error :".MySQL_Error());
  77.     $error->PrintDebug("No se puede conectar a la base de datos",1);
  78.     die();
  79. }
  80. else
  81. {
  82.     $error->PrintDebug("MySQL Connected",1);
  83.    
  84.     $dblink=mysql_select_db($dbname);
  85.     if(!$dbname)
  86.     {
  87.         trigger_error("Couldn't Select Database For Following Error :".MySQL_Error());
  88.         die();
  89.     }  
  90.     else
  91.     {
  92.         $error->PrintDebug("Database Selected",1);
  93.     }
  94. }
  95. ?>


Si os fijáis cada vez que da el error esta se repite 2 veces una por esa linea

$conn=mysql_connect('localhost','root','12345');

y otra por esta

trigger_error("Couldn't Connect To MySQL Because Of Following Error :".MySQL_Error());

Y el código de error son distintos, en uno devuelve un 2 y en otra el 1024...

¿Por qué, si son el mismo error no?
¿Cual de los errores es el bueno xD? se puede hacer que solo devuelva el segundo?
Lo intenté con $conn=@mysql_connect('localhost','root','12345'); pero creo que la @ es para que no aparezca el error por pantalla, en el fichero de log lo escribe igual, correcto?


Resultado:
Código:
An error occured on 15:10:50 - 13/02/10
Details are as follows
Error Number : 2
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'root'@'localhost' (using password: YES)
Occured in C:\AppServ\www\00-laboratorio_test\secciones\errores_php2.php
On Line 72 

--------------------------------------------------
An error occured on 15:10:50 - 13/02/10
Details are as follows
Error Number : 1024
Couldn't Connect To MySQL Because Of Following Error :Access denied for user 'root'@'localhost' (using password: YES)
Occured in C:\AppServ\www\00-laboratorio_test\secciones\errores_php2.php
On Line 76 

--------------------------------------------------
An error occured on 15:11:37 - 13/02/10
Details are as follows
Error Number : 2
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'root'@'localhost' (using password: YES)
Occured in C:\AppServ\www\00-laboratorio_test\secciones\errores_php2.php
On Line 72 

--------------------------------------------------
An error occured on 15:11:37 - 13/02/10
Details are as follows
Error Number : 1024
Couldn't Connect To MySQL Because Of Following Error :Access denied for user 'root'@'localhost' (using password: YES)
Occured in C:\AppServ\www\00-laboratorio_test\secciones\errores_php2.php
On Line 76 

--------------------------------------------------
An error occured on 15:12:26 - 13/02/10
Details are as follows
Error Number : 2
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'root'@'localhost' (using password: YES)
Occured in C:\AppServ\www\00-laboratorio_test\secciones\errores_php2.php
On Line 72 

--------------------------------------------------
An error occured on 15:12:26 - 13/02/10
Details are as follows
Error Number : 1024
Couldn't Connect To MySQL Because Of Following Error :Access denied for user 'root'@'localhost' (using password: YES)
Occured in C:\AppServ\www\00-laboratorio_test\secciones\errores_php2.php
On Line 76 

--------------------------------------------------
Muchas gracias de antemano!