Example #1
0
 public function processRecord(array $record)
 {
     if (empty($record['component'])) {
         $record['component'] = $this->componentName;
     }
     $record['runId'] = $this->runId;
     $record['pid'] = getmypid();
     $record['priority'] = $record['level_name'];
     if ($this->tokenData) {
         $record['token'] = ['id' => $this->tokenData['id'], 'description' => $this->tokenData['description'], 'token' => $this->tokenData['token'], 'owner' => ['id' => $this->tokenData['owner']['id'], 'name' => $this->tokenData['owner']['name']]];
     }
     if (isset($record['context']['exceptionId'])) {
         $record['exceptionId'] = $record['context']['exceptionId'];
     }
     if (isset($record['context']['exception'])) {
         /** @var \Exception $e */
         $e = $record['context']['exception'];
         if ($e instanceof \Exception) {
             $flattenException = FlattenException::create($e);
             $eHandler = new ExceptionHandler(true);
             $html = $eHandler->getHtml($flattenException);
             $record['exception'] = ['class' => get_class($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), 'attachment' => $this->s3Uploader->uploadString('exception', $html, 'text/html')];
         }
     }
     $json = json_encode($record);
     if (strlen($json) < 1024) {
         return $record;
     } else {
         $record['attachment'] = $this->s3Uploader->uploadString('log', $json, 'text/json');
         if (mb_strlen($record['message']) > 256) {
             $record['message'] = mb_substr($record['message'], 0, 256);
         }
         $allowedFields = ['message', 'component', 'runId', 'pid', 'priority', 'level', 'attachment', 'exception', 'exceptionId', 'token', 'cliCommand', 'http', 'job', 'app'];
         foreach (array_keys($record) as $fieldName) {
             if (!in_array($fieldName, $allowedFields)) {
                 unset($record[$fieldName]);
             }
         }
         if (isset($record['http'])) {
             $allowedFields = ['url', 'userAgent', 'ip'];
             foreach (array_keys($record['http']) as $fieldName) {
                 if (!in_array($fieldName, $allowedFields)) {
                     unset($record['http'][$fieldName]);
                 }
             }
         }
         if (isset($record['job'])) {
             $allowedFields = ['id'];
             foreach (array_keys($record['job']) as $fieldName) {
                 if (!in_array($fieldName, $allowedFields)) {
                     unset($record['job'][$fieldName]);
                 }
             }
         }
         return $record;
     }
 }
Example #2
0
 public function testGetContent()
 {
     $array = array_fill(0, 1000, "dummy stuff");
     $largeErrorContent = json_encode($array);
     $exception = new \Exception("error message [0]: " . $largeErrorContent, 500);
     for ($i = 1; $i <= 50; $i++) {
         $prevException = $exception;
         $exception = new \Exception("error message [{$i}]: " . $largeErrorContent, 500, $prevException);
     }
     $flattenException = \Keboola\Syrup\Debug\Exception\FlattenException::create($exception);
     $exceptionHandler = new \Keboola\Syrup\Debug\ExceptionHandler();
     $content = $exceptionHandler->getContent($flattenException);
     $found = strstr($content, "exceptions truncated");
     $this->assertNotFalse($found);
 }