Exemplo n.º 1
0
 /**
  * Register system log instance to phramework
  * @param null|object $additionalParameters
  * @throws Exception
  * @return boolean
  */
 public function register($additionalParameters = null)
 {
     if ($additionalParameters && !is_object($additionalParameters)) {
         throw new \Exception('additionalParameters must be an object');
     }
     $settings = $this->settings;
     //Ignore registration if disabled setting is set to true
     if (isset($this->settings->disabled) && $this->settings->disabled) {
         return false;
     }
     $logMatrix = isset($settings->matrix) ? (array) $settings->matrix : [];
     $logMatrixException = isset($settings->{'matrix-exception'}) ? (array) $settings->{'matrix-exception'} : [];
     $logObject = $this->logObject;
     /*
      * Register step callbacks
      */
     //Register after call URIStrategy (after controller/method is invoked) callback
     Phramework::$stepCallback->add(StepCallback::STEP_AFTER_CALL_URISTRATEGY, function ($step, $params, $HTTPMethod, $headers, $callbackVariables, $invokedController, $invokedMethod) use($logObject, $logMatrix, $additionalParameters, $settings) {
         $matrixKey = trim($invokedController, '\\') . '::' . $invokedMethod;
         $flags = isset($logMatrix[$matrixKey]) ? $logMatrix[$matrixKey] : self::LOG_STANDARD;
         //If ignore flag is active, dont store anything
         if ($flags === self::LOG_IGNORE) {
             return false;
         }
         //For common properties
         $object = SystemLog::prepareObject($flags, $settings, $params, $HTTPMethod, $headers, $additionalParameters);
         //Write specific
         return $logObject->log($step, $object);
     });
     //Register on error callback
     Phramework::$stepCallback->add(StepCallback::STEP_ERROR, function ($step, $params, $HTTPMethod, $headers, $callbackVariables, $errors, $code, $exception) use($logObject, $logMatrixException, $additionalParameters, $settings) {
         $matrixKey = trim(get_class($exception), '\\');
         $flags = isset($logMatrixException[$matrixKey]) ? $logMatrixException[$matrixKey] : self::LOG_STANDARD;
         //If ignore flag is active, dont store anything
         if ($flags === self::LOG_IGNORE) {
             return false;
         }
         //For common properties
         $object = SystemLog::prepareObject($flags, $settings, $params, $HTTPMethod, $headers, $additionalParameters);
         //Write specific
         $object->errors = $errors;
         $object->exception = serialize(self::flattenExceptionBacktrace($exception));
         $object->exception_class = $matrixKey;
         $debugBacktrace = (array) (object) $exception;
         if (isset($debugBacktrace["Exceptiontrace"])) {
             //Get call trace from exception
             $debugBacktrace = $debugBacktrace["Exceptiontrace"];
             foreach ($debugBacktrace as $k => &$v) {
                 if (isset($v['class'])) {
                     $v = $v['class'] . '::' . $v['function'];
                 } else {
                     $v = $v['function'];
                 }
             }
             $object->call_trace = $debugBacktrace;
         }
         return $logObject->log($step, $object);
     });
     return true;
 }