Improve record log

This commit is contained in:
sparkyx 2026-05-21 07:21:07 +02:00
parent 552a980182
commit 3991ee6af7
2 changed files with 96 additions and 55 deletions

View file

@ -1390,7 +1390,7 @@ function is_msie()
* Record also the GET and POST data * Record also the GET and POST data
* @param $p_message string message to display * @param $p_message string message to display
*/ */
function record_log($p_message) function record_log(...$a_message)
{ {
$date= date ('Y-m-d'); $date= date ('Y-m-d');
// variable: $handle_log resource on log file , // variable: $handle_log resource on log file ,
@ -1400,61 +1400,80 @@ function record_log($p_message)
{ {
if ( isset ($_SERVER['REMOTE_ADDR']) ) error_log(sprintf("origin %s\n",$_SERVER['REMOTE_ADDR'])); if ( isset ($_SERVER['REMOTE_ADDR']) ) error_log(sprintf("origin %s\n",$_SERVER['REMOTE_ADDR']));
if ( isset ($_SERVER['HTTP_USER_AGENT']) ) error_log(sprintf("agent %s\n",$_SERVER['HTTP_USER_AGENT'])); if ( isset ($_SERVER['HTTP_USER_AGENT']) ) error_log(sprintf("agent %s\n",$_SERVER['HTTP_USER_AGENT']));
if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) { foreach ($a_message as $p_message )
$exc=$p_message; {
do { if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
error_log("noalyss exception File [".$exc->getFile().":".$exc->getLine()."]",0); $exc=$p_message;
error_log("noalyss exception Message [".$exc->getMessage()."]",0); do {
error_log("noalyss exception Code [".$exc->getCode()."]",0); error_log("noalyss exception File [".$exc->getFile().":".$exc->getLine()."]",0);
error_log("noalyss exception Trace ".$exc->getTraceAsString(),0); error_log("noalyss exception Message [".$exc->getMessage()."]",0);
error_log("------ ",0); error_log("noalyss exception Code [".$exc->getCode()."]",0);
$exc=$exc->getPrevious(); error_log("noalyss exception Trace ".$exc->getTraceAsString(),0);
if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n"); error_log("------ ",0);
} while ($exc != null); $exc=$exc->getPrevious();
} else { if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
error_log("noalyss".var_export($p_message,true),0); } while ($exc != null);
} elseif ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
{
$var->formatOutput=true;
error_log("noalyss".$var->saveXML(),0);
} else
error_log("noalyss".var_export($p_message,true),0);
}
}
error_log("noalyss GET [".json_encode($_GET,0,10)."]"); error_log("noalyss GET [".json_encode($_GET,0,10)."]");
error_log("_POST [".json_encode($_POST,0,10)."]",0); error_log("_POST [".json_encode($_POST,0,10)."]",0);
} else { } else {
if ( isset ($_SERVER['REMOTE_ADDR']) ) fwrite($handle_log,sprintf("origin %s\n",$_SERVER['REMOTE_ADDR'])); foreach ($a_message as $p_message )
if ( isset ($_SERVER['HTTP_USER_AGENT']) ) fwrite($handle_log,sprintf("agent %s\n",$_SERVER['HTTP_USER_AGENT'])); {
/**
* write in syslog
*/
if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) { error_log("noalyss exception ".$p_message->getMessage(),0);
error_log("noalyss exception".$p_message->getTraceAsString(),0);
error_log("noalyss exception ".$p_message->getMessage(),0); } else {
error_log("noalyss exception".$p_message->getTraceAsString(),0); error_log("noalyss".var_export($p_message,true),0);
} else {
error_log("noalyss".var_export($p_message,true),0);
}
} }
$now=date ('Y-m-d H:i:s'); $now=date ('Y-m-d H:i:s');
fwrite ($handle_log,str_repeat("=", 80)."\n"); fwrite ($handle_log,str_repeat("=", 80)."\n");
if ( isset ($_SERVER['REMOTE_ADDR']) ) fwrite($handle_log,sprintf("origin %s\n",$_SERVER['REMOTE_ADDR']));
if ( isset ($_SERVER['HTTP_USER_AGENT']) ) fwrite($handle_log,sprintf("agent %s\n",$_SERVER['HTTP_USER_AGENT']));
fwrite ($handle_log,"ERROR: {$now}\n"); fwrite ($handle_log,"ERROR: {$now}\n");
fwrite($handle_log,"noalyss GET [".var_export($_GET,true)."]"); fwrite($handle_log,"noalyss GET [".var_export($_GET,true)."]");
fwrite ($handle_log,"\n"); fwrite ($handle_log,"\n");
fwrite($handle_log,"_POST [".var_export($_POST,true)."]"); fwrite($handle_log,"_POST [".var_export($_POST,true)."]");
fwrite ($handle_log,"\n"); fwrite ($handle_log,"\n");
if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) { foreach ($a_message as $p_message)
$exc=$p_message; {
do { if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
fwrite($handle_log,"noalyss exception File [".$exc->getFile().":".$exc->getLine()."]"); $exc=$p_message;
do {
fwrite($handle_log,"noalyss exception File [".$exc->getFile().":".$exc->getLine()."]");
fwrite ($handle_log,"\n");
fwrite($handle_log,"noalyss exception Message [".$exc->getMessage()."]");
fwrite ($handle_log,"\n");
fwrite($handle_log,"noalyss exception Code [".$exc->getCode()."]");
fwrite ($handle_log,"\n");
fwrite($handle_log,"noalyss exception Trace \n".$exc->getTraceAsString());
fwrite ($handle_log,"\n");
$exc=$exc->getPrevious();
if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
} while ($exc != null);
} elseif ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
{
$var->formatOutput=true;
$output.=$var->saveXML() .PHP_EOL;
} else {
fwrite($handle_log,"noalyss".var_export($p_message,true));
fwrite ($handle_log,"\n"); fwrite ($handle_log,"\n");
fwrite($handle_log,"noalyss exception Message [".$exc->getMessage()."]");
fwrite ($handle_log,"\n");
fwrite($handle_log,"noalyss exception Code [".$exc->getCode()."]");
fwrite ($handle_log,"\n");
fwrite($handle_log,"noalyss exception Trace \n".$exc->getTraceAsString());
fwrite ($handle_log,"\n");
$exc=$exc->getPrevious();
if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
} while ($exc != null);
} else {
fwrite($handle_log,"noalyss".var_export($p_message,true));
fwrite ($handle_log,"\n");
}
} }
fwrite ($handle_log,str_repeat("=", 80)."\n"); fwrite ($handle_log,str_repeat("=", 80)."\n");
@ -1462,27 +1481,43 @@ function record_log($p_message)
} }
} }
if(!function_exists('tracedebug')) { if(!function_exists('tracedebug')) {
function tracedebug($file,$var, $label = NULL) { function tracedebug($file,...$a_var) {
$tmp_file = sys_get_temp_dir().DIRECTORY_SEPARATOR.$file; $tmp_file = sys_get_temp_dir().DIRECTORY_SEPARATOR.$file;
$file_loginput=fopen( $tmp_file,'a+'); $file_loginput=fopen( $tmp_file,'a+');
if ( $file_loginput == false) { return;} if ( $file_loginput == false) { return;}
$output = ''; $output = '';
$output .= date('d-m-y H:i'); $output .= date('d-m-y H:i');
if(!is_null($label)) { foreach ($a_var as $var)
$output .= $label . ': ';
}
if ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
{ {
$var->formatOutput=true; if ( gettype ($var) == "object" && method_exists($var,"getTraceAsString") == 1) {
$output.=$var->saveXML() .PHP_EOL; $exc=$var;
} else do {
{ fwrite($file_loginput,"noalyss exception File [".$exc->getFile().":".$exc->getLine()."]");
$output .= print_r($var, 1) . PHP_EOL; fwrite ($file_loginput,"\n");
fwrite($file_loginput,"noalyss exception Message [".$exc->getMessage()."]");
fwrite ($file_loginput,"\n");
fwrite($file_loginput,"noalyss exception Code [".$exc->getCode()."]");
fwrite ($file_loginput,"\n");
fwrite($file_loginput,"noalyss exception Trace \n".$exc->getTraceAsString());
fwrite ($file_loginput,"\n");
$exc=$exc->getPrevious();
if ($exc != null )fwrite ($file_loginput,"*********************** Previous *********************** \n");
} while ($exc != null);
}elseif ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
{
$var->formatOutput=true;
$output.=$var->saveXML() .PHP_EOL;
} else
{
$output .= print_r($var, 1) . PHP_EOL;
}
$output.= str_repeat("-", 80);
$output.="\n";
} }
file_put_contents($tmp_file, $output, FILE_APPEND); file_put_contents($tmp_file, $output, FILE_APPEND);
} }
} }

View file

@ -42,7 +42,13 @@ function x()
try { try {
x(); x();
} catch (\Exception $e) { } catch (\Exception $e) {
record_log($e); record_log($e);
tracedebug("log",$e);
} }
printf("tracedebug in sys_get_temp_dir() = %s", sys_get_temp_dir());
?>
<p>
Check file in noalyss/log
</p>