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<?
/*******************************************************************************
Program Name : Error.php
Purpose : This file contains the error class,which manages errors.
It handles erros in customized way
Creation Date : 04/22/2005 02:05PM
********************************************************************************/
class Error
{
var $logfile;
var $debug;
/*==============================================================================
Constructor
Arguments :- $debug_yesno. If 1 then errors will be logged,else displayed
==============================================================================*/
function Error($debug_yesno=0,$logfile_path)
{
$this->debug=$debug_yesno;
$this->printdebug=1;
//Report only user generated error messages
if($this->debug>=1)
{
$this->logfile=$logfile_path;
}
else
{
}
}
/*==============================================================================
Following function logs an error message
==============================================================================*/
function ErrorHandler($errno,$errstr,$errfile,$errline)
{
$errtime=date("H:i:s - d/m/y"); $error_message="An error occured on ".$errtime."\n";
$error_message .="Details are as follows\n";
$error_message .="Error Number : $errno\n";
$error_message .=$errstr."\nOccured in ".$errfile."\n";
$error_message .="On Line $errline \n\n";
$error_message .="\n";
$fp=fopen($this->logfile,"a"); }
/*==============================================================================
Following function prints debug messages on the screen
==============================================================================*/
function PrintDebug($message,$yesno=0)
{
if($yesno>0)
{
$message="<font face=\"verdana\" size=\"2\" color=\"red\">Debug : ".$message."</font><br>";
print $message;
}
}
}
?>
<?
//If initialized with 1 errors will be logged. else they will be displayed on the screen
$error=new Error(1,"err.log.txt");
if(!$conn)
{
$error->PrintDebug("No se puede conectar a la base de datos",1);
}
else
{
$error->PrintDebug("MySQL Connected",1);
if(!$dbname)
{
}
else
{
$error->PrintDebug("Database Selected",1);
}
}
?>
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!