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,6 +1400,8 @@ 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']));
foreach ($a_message as $p_message )
{
if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) { if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
$exc=$p_message; $exc=$p_message;
do { do {
@ -1411,16 +1413,22 @@ function record_log($p_message)
$exc=$exc->getPrevious(); $exc=$exc->getPrevious();
if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n"); if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
} while ($exc != null); } while ($exc != null);
} else { } 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".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->getMessage(),0);
@ -1429,14 +1437,20 @@ function record_log($p_message)
error_log("noalyss".var_export($p_message,true),0); 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");
foreach ($a_message as $p_message)
{
if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) { if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
$exc=$p_message; $exc=$p_message;
do { do {
@ -1451,30 +1465,49 @@ function record_log($p_message)
$exc=$exc->getPrevious(); $exc=$exc->getPrevious();
if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n"); if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
} while ($exc != null); } while ($exc != null);
} elseif ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
{
$var->formatOutput=true;
$output.=$var->saveXML() .PHP_EOL;
} else { } else {
fwrite($handle_log,"noalyss".var_export($p_message,true)); fwrite($handle_log,"noalyss".var_export($p_message,true));
fwrite ($handle_log,"\n"); fwrite ($handle_log,"\n");
} }
}
fwrite ($handle_log,str_repeat("=", 80)."\n"); fwrite ($handle_log,str_repeat("=", 80)."\n");
fclose($handle_log); fclose($handle_log);
} }
} }
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" && method_exists($var,"getTraceAsString") == 1) {
if ( gettype ($var) == 'object' && get_class($var)=='DOMDocument') $exc=$var;
do {
fwrite($file_loginput,"noalyss exception File [".$exc->getFile().":".$exc->getLine()."]");
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; $var->formatOutput=true;
$output.=$var->saveXML() .PHP_EOL; $output.=$var->saveXML() .PHP_EOL;
@ -1482,7 +1515,9 @@ if(!function_exists('tracedebug')) {
{ {
$output .= print_r($var, 1) . PHP_EOL; $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>