logger : add the support to catch all the trigger_error messages

logger : add the backtraceDisplay conditionnal


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2231 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-08-13 12:43:56 +00:00
parent 93cf28ebfb
commit c08d504486

View File

@@ -17,9 +17,9 @@ class logger
$d->logwrite ("Super log NOTICE", $d::NOTICE); */
// TODO : Add SQL support
/** The method to log.
Can be display, file, syslog
Can be display, file, syslog, stderr
Can be merged with a pipe : "display|syslog|file" or put in array */
public $logtype = "display";
public $logtype = "stderr";
/** For logtype=file, the filename to use */
public $logfile = FALSE;
/** Timezone use to save the logs */
@@ -34,6 +34,8 @@ class logger
/** Remove X entries of backtrace to see the right file generating the error
*/
public $backTraceSkip = 0;
/** Display the backtrace in all the messages */
public $backtraceDisplay = false;
/** The priorities which can be used in the priorities
LOG_EMERG system is unusable
@@ -53,6 +55,41 @@ class logger
LOG_INFO => "INFO",
LOG_DEBUG => "DEBUG");
/** Catch all the messages raised by the PHP, in trigger_error
Use the properties of the logger to save the logs */
public function catchAll ()
{
set_error_handler (array (&$this, "errorHandler"));
}
public function errorHandler ($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno))
{
// This error code is not included in error_reporting
return;
}
switch($errno)
{
case E_ERROR: $priority = LOG_ERR; break;
case E_WARNING: $priority = LOG_WARNING; break;
case E_PARSE: $priority = LOG_PARSE; break;
case E_NOTICE: $priority = LOG_NOTICE; break;
case E_CORE_ERROR: $priority = LOG_ERR; break;
case E_CORE_WARNING: $priority = LOG_WARNING; break;
case E_COMPILE_ERROR: $priority = LOG_ERR; break;
case E_COMPILE_WARNING: $priority = LOG_WARNING; break;
case E_USER_ERROR: $priority = LOG_ERR; break;
case E_USER_WARNING: $priority = LOG_WARNING; break;
case E_USER_NOTICE: $priority = LOG_NOTICE; break;
case E_STRICT: $priority = LOG_WARNING; break;
case E_RECOVERABLE_ERROR: $priority = LOG_ERR; break;
case E_DEPRECATED: $priority = LOG_WARNING; break;
case E_USER_DEPRECATED: $priority = LOG_WARNING; break;
}
$this->log ($priority, $errstr);
}
/** Store a new message log in the log manager defined by $logtype
The message can be multiple types. An array will be stored in textual form
but it will accept only one depth.
@@ -135,6 +172,13 @@ class logger
// Add the filename which generate the error
$msg .= " [".basename ($back["file"]).":".$back["line"]."]";
// Display the backtrace if it is needed
if ($this->backtraceDisplay)
{
$e = new Exception();
$msg .= "\n".($e->getTraceAsString());
}
if (is_string ($this->logtype))
$logsType = explode ("|", $this->logtype);
elseif (is_array ($this->logtype))
@@ -204,14 +248,10 @@ class logger
@param integer|null $priority Priority to use */
private function logstderr ($message, $priority)
{
if (php_sapi_name () !== "cli")
file_put_contents("php://stderr", "<tt>");
ini_set ("date.timezone", $this->timezone);
$message = date ("Y/m/d H:i:s")." [".$this->priorities[$priority]."] ".
$message;
file_put_contents("php://stderr", "$message");
if (php_sapi_name () !== "cli")
file_put_contents("php://stderr", "</tt><br/>");
file_put_contents("php://stderr", "\n");
}