improved system log, now also catching shutdown messages, cleaned code

This commit is contained in:
Ulf Gebhardt 2013-12-20 06:23:50 +01:00
parent 8b28ea441a
commit e0559012ef
15 changed files with 93 additions and 147 deletions

View File

@ -1,96 +0,0 @@
<?php
namespace SYSTEM\LOG;
class LOG {
//Singleton
private $handlers = array();
private static $instance = null;
public static function getInstance(){
if (null === self::$instance) {
self::$instance = new self;}
return self::$instance;
}
private function __construct(){}
private function __clone(){}
//$handler = string with classname
public function registerHandler($handler){
if( !class_exists($handler) ||
!\method_exists($handler,'MASK') ||
!\method_exists($handler,'CALL')){
die("You registered an invalid Errorhandler!");}
$this->handlers[] = $handler;
set_error_handler('\SYSTEM\LOG\LOG::__error_handler');
set_exception_handler('\SYSTEM\LOG\LOG::__exception_handler');
}
private function call_handlers(\Exception $E, $errno, $thrown = false){
foreach($this->handlers as $handler){
if( ((call_user_func(array($handler,'MASK')) & $errno)) &&
call_user_func_array(array($handler,'CALL'),array($E,$errno, $thrown))){
return true;
}
}
return false;
}
//Static
//Exceptions
public static function __exception_handler(\Exception $E, $thrown = true){
if($E instanceof \SYSTEM\LOG\ERROR) { return self::LOG_EXCEPTION($E, E_USER_ERROR, $thrown);} //Dies
if($E instanceof \SYSTEM\LOG\WARNING) { return self::LOG_EXCEPTION($E, E_USER_WARNING, $thrown);} //Continues
if($E instanceof \SYSTEM\LOG\INFO) { return self::LOG_EXCEPTION($E, E_NOTICE, $thrown);} //Continues
if($E instanceof \SYSTEM\LOG\DEPRECATED){ return self::LOG_EXCEPTION($E, E_USER_DEPRECATED, $thrown);} //Continues
self::LOG_EXCEPTION($E, E_USER_ERROR, $thrown); //Dies
}
//Triggers error, if not handled by registered Handlers(double check -> but only this way we can keep the original Exception class)
protected static function LOG_EXCEPTION(\Exception $E,$errno = E_USER_ERROR, $thrown = false){
self::__error_handler_e($E, $errno, $thrown);}
//Errors
public static function __error_handler($errno, $errstr, $errfile, $errline){
$e = new \SYSTEM\LOG\ERROR($errstr,$errno);
return self::__error_handler_e($e ,$errno, true);}
public static function __error_handler_e(\Exception $E, $errno, $thrown){
switch ($errno) {
//Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
case E_ERROR: return self::_DIE($E,$errno,true);
//Run-time warnings (non-fatal errors). Execution of the script is not halted.
case E_WARNING: return self::_CONTINUE($E,$errno,true);
//Compile-time parse errors. Parse errors should only be generated by the parser.
case E_PARSE: return self::_DIE($E,$errno,true);
//Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
case E_NOTICE: return self::_CONTINUE($E,$errno,false | $thrown);
//Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
case E_CORE_ERROR: return self::_DIE($E,$errno,true);
//Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
case E_CORE_WARNING: return self::_CONTINUE($E,$errno,true);
//Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
case E_COMPILE_ERROR: return self::_DIE($E,$errno,true);
//Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
case E_COMPILE_WARNING: return self::_CONTINUE($E,$errno,true);
//User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
case E_USER_ERROR: return self::_DIE($E,$errno,false | $thrown);
//User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
case E_USER_WARNING: return self::_CONTINUE($E,$errno,false | $thrown);
//Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. Since PHP 5 but not included in E_ALL until PHP 5.4.0 rated in PHP code by using the PHP function trigger_error().
case E_STRICT: return self::_CONTINUE($E,$errno,true);
//Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR. Since PHP 5.2.0
case E_RECOVERABLE_ERROR: return self::_CONTINUE($E,$errno,true);
//Run-time notices. Enable this to receive warnings about code that will not work in future versions. Since PHP 5.3.0
case E_DEPRECATED: return self::_CONTINUE($E,$errno,true);
//User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error(). Since PHP 5.3.0
case E_USER_DEPRECATED: return self::_CONTINUE($E,$errno,false | $thrown);
default: return false; //Not handled Error use std php handler
}
}
private static function _DIE(\Exception $E, $errno, $thrown = false){
if(self::getInstance()->call_handlers($E, $errno, $thrown)){die();}}
private static function _CONTINUE(\Exception $E, $errno, $thrown = false){
return self::getInstance()->call_handlers($E, $errno, $thrown);}
}

View File

@ -7,6 +7,6 @@ class error_handler {
public static function MASK(){
throw new \RuntimeException("Implement this");}
//Errorhandler
public static function CALL(\Exception $E, $errno, $thrown){
public static function CALL(\Exception $E, $thrown){
throw new \RuntimeException("Implement this");}
}

View File

@ -5,8 +5,11 @@ namespace SYSTEM\LOG;
//Register this before every other handler, cuz this will need to handle every single error.
//And only the first ErrorHandler will be called if he returns true in CALL.
class error_handler_dbwriter extends \SYSTEM\LOG\error_handler {
public static function CALL(\Exception $E, $errno, $thrown){
try{
public static function CALL(\Exception $E, $thrown){
try{
if(\property_exists(get_class($E), 'logged') && $E->logged){
return false;} //alrdy logged
if(\SYSTEM\system::isSystemDbInfoPG()){
$con = new \SYSTEM\DB\Connection(\SYSTEM\system::getSystemDBInfo());
$con->prepare( 'sysLogStmt', 'INSERT INTO system.sys_log '.
@ -22,6 +25,9 @@ class error_handler_dbwriter extends \SYSTEM\LOG\error_handler {
array( get_class($E), $E->getMessage(), $E->getCode(), $E->getFile(), $E->getLine(), $E->getTraceAsString(),
getenv('REMOTE_ADDR'),round(microtime(true) - \SYSTEM\time::getStartTime(),5),microtime(true)));
}
if(\property_exists(get_class($E), 'logged')){
$E->logged = true;} //we just did log
} catch (\Exception $E){} //Error -> Ignore
return false; //We just log and do not handle the error!

View File

@ -5,11 +5,12 @@ namespace SYSTEM\LOG;
class error_handler_jsonoutput extends \SYSTEM\LOG\error_handler {
//Only those who die!
public static function MASK(){ return \E_ALL;} //\E_ERROR | \E_USER_ERROR | \E_CORE_ERROR | \E_COMPILE_ERROR; }
public static function CALL(\Exception $E, $errno, $thrown){
if($thrown){
//TODO move jsonresult into system
echo \SYSTEM\LOG\JsonResult::error($E);
die(); //we can have only one json result per page call else -> multiple headers are sent
public static function CALL(\Exception $E, $thrown){
if($thrown){
try{
echo \SYSTEM\LOG\JsonResult::error($E);
} catch (\Exception $E){} //Error -> Ignore
return true;
}
}
}

View File

@ -1,10 +1,4 @@
<?php
namespace SYSTEM\LOG;
class DEPRECATED extends \Exception {
public function __construct($message = "", $code = 0, $previous = NULL){
parent::__construct($message, $code, $previous);
\SYSTEM\LOG\LOG::__exception_handler($this,false);
}
}
class DEPRECATED extends \SYSTEM\LOG\SYSTEM_EXCEPTION {}

View File

@ -1,10 +1,4 @@
<?php
namespace SYSTEM\LOG;
class ERROR extends \Exception {
public function __construct($message = "", $code = 0, $previous = NULL){
parent::__construct($message, $code, $previous);
\SYSTEM\LOG\LOG::__exception_handler($this,false);
}
}
class ERROR extends \SYSTEM\LOG\SYSTEM_EXCEPTION {}

View File

@ -1,11 +1,5 @@
<?php
namespace SYSTEM\LOG;
class INFO extends \Exception {
public function __construct($message = "", $code = 0, $previous = NULL){
parent::__construct($message, $code, $previous);
\SYSTEM\LOG\LOG::__exception_handler($this,false);
}
}
class INFO extends \SYSTEM\LOG\SYSTEM_EXCEPTION {}

View File

@ -0,0 +1,11 @@
<?php
namespace SYSTEM\LOG;
class SYSTEM_EXCEPTION extends \Exception {
public $logged = false;
public function __construct($message = "", $code = 1, $previous = NULL){
parent::__construct($message, $code, $previous);
\SYSTEM\LOG\log::__exception_handler($this,false);
}
}

View File

@ -1,17 +1,13 @@
<?php
namespace SYSTEM\LOG;
class TranslatableError extends \SYSTEM\LOG\ERROR {
public function __construct($string_id, $code = 0, $previous = NULL , $locale = NULL){
$message = \SYSTEM\locale::getStrings(array($string_id), $locale);
//print_r($message);
public function __construct($string_id, $code = 0, $previous = NULL , $locale = NULL){
$message = \SYSTEM\locale::getStrings(array($string_id), $locale);
if(!isset($message[$string_id])){
throw new \SYSTEM\LOG\ERROR("Could not retrive Errortranslation: ".$string_id);}
parent::__construct($message[$string_id], $code, $previous);
\SYSTEM\LOG\LOG::__exception_handler($this,false);
parent::__construct($message[$string_id], $code, $previous);
}
}

View File

@ -1,10 +1,4 @@
<?php
namespace SYSTEM\LOG;
class WARNING extends \Exception {
public function __construct($message = "", $code = 0, $previous = NULL){
parent::__construct($message, $code, $previous);
\SYSTEM\LOG\LOG::__exception_handler($this,false);
}
}
class WARNING extends \SYSTEM\LOG\SYSTEM_EXCEPTION {}

43
log/log.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace SYSTEM\LOG;
class log {
const HANDLER_FUNC_MASK = 'MASK';
const HANDLER_FUNC_CALL = 'CALL';
private static $handlers = array();
//$handler = string with classname
public static function registerHandler($handler){
if( !class_exists($handler) ||
!\method_exists($handler,self::HANDLER_FUNC_MASK) ||
!\method_exists($handler,self::HANDLER_FUNC_CALL)){
die("You registered an invalid Errorhandler!");}
self::$handlers[] = $handler;
set_error_handler('\SYSTEM\LOG\log::__error_handler');
set_exception_handler('\SYSTEM\LOG\log::__exception_handler');
register_shutdown_function( "\SYSTEM\LOG\log::__shutdown_handler" );
}
private static function call_handlers(\Exception $E, $thrown = true){
foreach(self::$handlers as $handler){
if( ((\call_user_func(array($handler,self::HANDLER_FUNC_MASK)) & $E->getCode())) &&
\call_user_func_array(array($handler,self::HANDLER_FUNC_CALL),array($E, $thrown))){
return true;}
}
return false;
}
public static function __exception_handler(\Exception $E, $thrown = true){
return self::call_handlers($E, $thrown) && $thrown;}
public static function __error_handler($code, $message, $file, $line, $thrown = true){
return self::call_handlers(new \ErrorException($message, 1, $code, $file, $line) ,$thrown);}
public static function __shutdown_handler($thrown = true) {
if( ($error = error_get_last()) !== NULL && !$error['type'] === E_DEPRECATED) { //http://www.dreamincode.net/forums/topic/284506-having-trouble-supressing-magic-quotes-gpc-fatal-errors/
return self::call_handlers(new \ErrorException($error["message"], 1, $error["type"],$error["file"],$error["line"]) ,$thrown);}
}
}

View File

@ -1,2 +1,2 @@
<?php
\SYSTEM\LOG\LOG::getInstance()->registerHandler('\SYSTEM\LOG\error_handler_dbwriter');
\SYSTEM\LOG\log::registerHandler('\SYSTEM\LOG\error_handler_dbwriter');

View File

@ -1,2 +1,2 @@
<?php
\SYSTEM\LOG\LOG::getInstance()->registerHandler('\SYSTEM\LOG\error_handler_jsonoutput');
\SYSTEM\LOG\log::registerHandler('\SYSTEM\LOG\error_handler_jsonoutput');

View File

@ -1,14 +1,22 @@
<?php
namespace SYSTEM;
class HEADER {
private static function checkHeader(){
$file = null;
$line = null;
if(headers_sent(&$file, &$line)){
throw new \SYSTEM\LOG\ERROR('Header already sent @ '.$file.' line '.$line);}
}
public static function JSON(){
header('content-type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods", "POST, GET, OPTIONS');
header('Access-Control-Allow-Headers *');
self::checkHeader();
header('content-type: application/json');
//header('Access-Control-Allow-Methods", "POST, GET, OPTIONS');
//header('Access-Control-Allow-Headers *');
}
public static function PNG(){
self::checkHeader();
header('content-type:image/png;');}
}

View File

@ -5,7 +5,8 @@ const C_ROOT = '<root>';
const C_SUBPATH = '<subpath>';
abstract class PATH {
abstract static public function getPath();
static public function getPath(){
throw new \RuntimeException("Not Implemented");}
}
class PROOT extends PATH {