Exemplo n.º 1
0
 public function testGetRecordContext()
 {
     $user = CMTest_TH::createUser();
     $httpRequest = CM_Http_Request_Abstract::factory('post', '/foo?bar=1&baz=quux&viewInfoList=fooBar', ['bar' => 'baz', 'host' => 'foo.bar:8080'], ['http_referer' => 'http://bar/baz', 'http_user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_10)', 'foo' => 'quux'], '{"foo" : "bar", "quux" : "baz"}');
     $clientId = $httpRequest->getClientId();
     $computerInfo = new CM_Log_Context_ComputerInfo('www.example.com', 'v7.0.1');
     $exception = new CM_Exception_Invalid('Bad', null, ['foo' => 'bar']);
     $context = new CM_Log_Context();
     $context->setExtra(['bar' => 'baz', 'baz' => 'quux']);
     $context->setUser($user);
     $context->setException($exception);
     $context->setComputerInfo($computerInfo);
     $context->setHttpRequest($httpRequest);
     $contextFormatter = new CM_Log_ContextFormatter_Cargomedia('appName');
     $formattedContext = $contextFormatter->formatContext($context);
     $this->assertSame('www.example.com', $formattedContext['computerInfo']['fqdn']);
     $this->assertSame('v7.0.1', $formattedContext['computerInfo']['phpVersion']);
     $this->assertSame('/foo?bar=1&baz=quux&viewInfoList=fooBar', $formattedContext['httpRequest']['uri']);
     $this->assertSame(join("\n", ['{', '    "bar": "1",', '    "baz": "quux",', '    "foo": "bar",', '    "quux": "baz"', '}']), $formattedContext['httpRequest']['query']);
     $this->assertSame('POST', $formattedContext['httpRequest']['method']);
     $this->assertSame('http://bar/baz', $formattedContext['httpRequest']['referer']);
     $this->assertSame('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_10)', $formattedContext['httpRequest']['useragent']);
     $this->assertSame('foo.bar', $formattedContext['httpRequest']['hostname']);
     $this->assertSame(['id' => $user->getId(), 'displayName' => 'user' . $user->getId()], $formattedContext['appName']['user']);
     $this->assertSame($clientId, $formattedContext['appName']['client']['id']);
     $this->assertSame('baz', $formattedContext['appName']['bar']);
     $this->assertSame('quux', $formattedContext['appName']['baz']);
     $this->assertSame('CM_Exception_Invalid', $formattedContext['exception']['type']);
     $this->assertSame('Bad', $formattedContext['exception']['message']);
     $this->assertArrayHasKey('stack', $formattedContext['exception']);
     $this->assertInternalType('string', $formattedContext['exception']['stack']);
     $this->assertSame(['foo' => "'bar'"], $formattedContext['exception']['metaInfo']);
     $this->assertRegExp('/library\\/CM\\/Log\\/ContextFormatter\\/CargomediaTest\\.php\\(\\d+\\)/', $formattedContext['exception']['stack']);
 }
Exemplo n.º 2
0
 /**
  * @param Swift_Mime_Message $message
  * @param array|null         $failedRecipients
  * @param Exception|null     $exception
  */
 protected function _logSendError(Swift_Mime_Message $message, array $failedRecipients = null, Exception $exception = null)
 {
     $context = new CM_Log_Context();
     $context->setExtra(['message' => ['subject' => $message->getSubject(), 'from' => $message->getFrom(), 'to' => $message->getTo(), 'cc' => $message->getCc(), 'bcc' => $message->getBcc()], 'failedRecipients' => $failedRecipients]);
     if ($exception) {
         $context->setException($exception);
     }
     $this->getServiceManager()->getLogger()->error('Failed to send email', $context);
 }
Exemplo n.º 3
0
 /**
  * @param string $className
  * @return CM_Jobdistribution_Job_Abstract|null
  */
 protected function _instantiateJob($className)
 {
     try {
         /** @var CM_Jobdistribution_Job_Abstract $job */
         $job = new $className();
         if ($job instanceof CM_Service_ManagerAwareInterface) {
             /** @var CM_Service_ManagerAwareInterface $job */
             $job->setServiceManager($this->getServiceManager());
         }
         return $job;
     } catch (Exception $e) {
         $logLevel = CM_Log_Logger::exceptionToLevel($e);
         $context = new CM_Log_Context();
         $context->setException($e);
         $this->getServiceManager()->getLogger()->addMessage('Delayed queue error', $logLevel, $context);
         return null;
     }
 }
Exemplo n.º 4
0
 /**
  * @param string  $streamChannelMediaId
  * @param CM_File $archiveSource
  * @throws CM_Exception_Invalid
  */
 public function importArchive($streamChannelMediaId, CM_File $archiveSource)
 {
     $streamChannelMediaId = (string) $streamChannelMediaId;
     $streamChannelArchive = CM_Model_StreamChannelArchive_Media::findByMediaId($streamChannelMediaId);
     if (!$streamChannelArchive) {
         $streamChannel = CM_Model_StreamChannel_Media::findByMediaId($streamChannelMediaId);
         if ($streamChannel) {
             throw new CM_Exception_Invalid('Archive not created, please try again later', null, ['streamChannelMediaId' => $streamChannelMediaId]);
         }
         $exception = new CM_Exception_Invalid('Archive not found, stream channel not found, skipping', CM_Exception::WARN, ['streamChannelMediaId' => $streamChannelMediaId]);
         $context = new CM_Log_Context();
         $context->setException($exception);
         $this->getServiceManager()->getLogger()->warning('Archive creating error', $context);
         return;
     }
     $filename = $streamChannelArchive->getId() . '-' . $streamChannelArchive->getHash() . '-original.' . $archiveSource->getExtension();
     $archiveDestination = new CM_File_UserContent('streamChannels', $filename, $streamChannelArchive->getId());
     $archiveDestination->ensureParentDirectory();
     $archiveSource->copyToFile($archiveDestination);
     $streamChannelArchive->setFile($archiveDestination);
 }
Exemplo n.º 5
0
 public function testWriteRecordWithContext()
 {
     $message = 'foo';
     $exceptionMessage = 'foo!';
     $extra = ['foo', 'bar' => true, 'foo' => ['foobar' => 1]];
     /** @var CM_OutputStream_Interface|Mocka\ClassMock $mockStreamInterface */
     $mockStreamInterface = $this->mockInterface('CM_OutputStream_Interface')->newInstanceWithoutConstructor();
     /** @var Mocka\FunctionMock $mockWritelnMethod */
     $mockWritelnMethod = $mockStreamInterface->mockMethod('writeln');
     $mockWritelnMethod->set(function ($outputText) use($message, $exceptionMessage, $extra) {
         $this->assertRegExp('/^\\[[0-9T\\:\\-\\+]+ - none - php none - INFO\\] ' . $message . '\\n/s', $outputText);
         $this->assertRegExp('/\\n - extra: ' . json_encode($extra, JSON_PRETTY_PRINT) . '/s', $outputText);
         $this->assertRegExp('/\\n - exception:(?:\\s+) - message: ' . $exceptionMessage . '.*$/s', $outputText);
     });
     $context = new CM_Log_Context();
     $context->setExtra($extra);
     $context->setException(new Exception($exceptionMessage));
     $record = new CM_Log_Record(CM_Log_Logger::INFO, $message, $context);
     $formatter = new CM_Log_Formatter_Text();
     $handler = new CM_Log_Handler_Stream($mockStreamInterface, $formatter);
     $this->forceInvokeMethod($handler, '_writeRecord', [$record]);
     $this->assertSame(1, $mockWritelnMethod->getCallCount());
 }
Exemplo n.º 6
0
 /**
  * @param Exception $exception
  */
 protected function _logException(Exception $exception)
 {
     $logLevel = CM_Log_Logger::exceptionToLevel($exception);
     $context = new CM_Log_Context();
     $context->setException($exception);
     CM_Service_Manager::getInstance()->getLogger()->addMessage('KickBox client error', $logLevel, $context);
 }
Exemplo n.º 7
0
 /**
  * @param Closure $regularCode
  * @param Closure $errorCode
  * @return mixed
  * @throws CM_Exception
  */
 protected function _runWithCatching(Closure $regularCode, Closure $errorCode)
 {
     try {
         return $regularCode();
     } catch (CM_Exception $ex) {
         $config = self::_getConfig();
         $exceptionsToCatch = $config->exceptionsToCatch;
         $catchPublicExceptions = !empty($config->catchPublicExceptions);
         $errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use($ex) {
             return is_a($ex, $exceptionClass);
         });
         $catchException = null !== $errorOptions;
         if ($catchException && isset($errorOptions['log']) && true === $errorOptions['log']) {
             $logLevel = isset($errorOptions['level']) ? $errorOptions['level'] : null;
             if (null === $logLevel) {
                 $logLevel = CM_Log_Logger::exceptionToLevel($ex);
             }
             $context = new CM_Log_Context();
             $context->setUser($this->getViewer());
             $context->setException($ex);
             $this->getServiceManager()->getLogger()->addMessage('Response processing error', $logLevel, $context);
         }
         if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) {
             $errorOptions = [];
             $catchException = true;
         }
         if ($catchException) {
             return $errorCode($ex, $errorOptions);
         }
         throw $ex;
     }
 }
Exemplo n.º 8
0
 public function testHandleException()
 {
     $mockLogHandler = $this->mockInterface('CM_Log_Handler_HandlerInterface')->newInstance();
     $mockHandleRecord = $mockLogHandler->mockMethod('handleRecord');
     $logger = $this->_getLoggerMock(new CM_Log_Context(), new CM_Log_Handler_Layered([new CM_Log_Handler_Layered_Layer([$mockLogHandler])]));
     $exception = new Exception('foo');
     $mockHandleRecord->set(function (CM_Log_Record $record) use($exception) {
         $this->assertTrue(!!$record->getContext()->getException());
         $recordException = $record->getContext()->getException();
         $this->assertSame('foo', $recordException->getMessage());
         $this->assertSame($exception->getLine(), $recordException->getLine());
         $this->assertSame($exception->getFile(), $recordException->getFile());
         $this->assertSame('Error happened', $record->getMessage());
         $this->assertSame(CM_Log_Logger::ERROR, $record->getLevel());
     });
     $context = new CM_Log_Context();
     $context->setException($exception);
     $logger->error('Error happened', $context);
     $this->assertSame(1, $mockHandleRecord->getCallCount());
     $exception = new CM_Exception('bar');
     $mockHandleRecord->set(function (CM_Log_Record $record) use($exception) {
         $recordException = $record->getContext()->getException();
         $this->assertSame('bar', $recordException->getMessage());
         $this->assertSame($exception->getLine(), $recordException->getLine());
         $this->assertSame($exception->getFile(), $recordException->getFile());
         $this->assertSame('Warning alert', $record->getMessage());
         $this->assertSame(CM_Log_Logger::WARNING, $record->getLevel());
     });
     $context = new CM_Log_Context();
     $context->setException($exception);
     $logger->warning('Warning alert', $context);
     $this->assertSame(2, $mockHandleRecord->getCallCount());
 }
Exemplo n.º 9
0
 public function testAggregate()
 {
     $client = $this->getServiceManager()->getMongoDb();
     $encoder = new CM_Log_Encoder_MongoDb();
     $handler = new CM_Log_Handler_MongoDb($client, $encoder, CM_Paging_Log::COLLECTION_NAME);
     $context1 = new CM_Log_Context();
     $context1->setExtra(['bar' => 'quux']);
     $record1 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'foo', $context1);
     $context2 = new CM_Log_Context();
     $record2 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'baz', $context2);
     $exception = new CM_Exception_Invalid('Bad news', CM_Exception::WARN, ['baz' => 'bar']);
     $context3 = new CM_Log_Context();
     $context3->setException($exception);
     $record3 = new CM_Log_Record(CM_Log_Logger::WARNING, 'bar', $context3);
     //they will not be found
     $handler->handleRecord($record1);
     $handler->handleRecord($record2);
     $handler->handleRecord($record3);
     CMTest_TH::timeDaysForward(2);
     //recreate records to correctly set up CM_Log_Record::createdAt
     $context1 = new CM_Log_Context();
     $context1->setExtra(['bar' => 'quux']);
     $record1 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'foo', $context1);
     $context3 = new CM_Log_Context();
     $context3->setException($exception);
     $record3 = new CM_Log_Record(CM_Log_Logger::WARNING, 'bar', $context3);
     $handler->handleRecord($record1);
     $handler->handleRecord($record3);
     $handler->handleRecord($record3);
     CMTest_TH::timeDaysForward(1);
     $exception2 = new CM_Exception_Invalid('Some info', CM_Exception::FATAL, ['foo' => 'bar']);
     //recreate records to correctly set up CM_Log_Record::createdAt
     $context1 = new CM_Log_Context();
     $context1->setExtra(['bar' => 'quux']);
     $record1 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'foo', $context1);
     $context2 = new CM_Log_Context();
     $record2 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'baz', $context2);
     $context3 = new CM_Log_Context();
     $context3->setException($exception);
     $record3 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'Error bar', $context3);
     $context4 = new CM_Log_Context();
     $context4->setException($exception2);
     $record4 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'Error bar', $context4);
     $handler->handleRecord($record2);
     $handler->handleRecord($record2);
     $handler->handleRecord($record2);
     $handler->handleRecord($record3);
     $handler->handleRecord($record3);
     $handler->handleRecord($record4);
     $handler->handleRecord($record1);
     $handler->handleRecord($record1);
     $handler->handleRecord($record1);
     $paging = new CM_Paging_Log([CM_Log_Logger::DEBUG], null, true, 2 * 86400);
     $this->assertSame(4, $paging->getCount());
     $foundRecord1 = $paging->getItem(0);
     $foundRecord2 = $paging->getItem(1);
     $foundRecord3 = $paging->getItem(2);
     $foundRecord4 = $paging->getItem(3);
     $this->assertSame(4, $foundRecord1['count']);
     $this->assertSame(3, $foundRecord2['count']);
     $this->assertSame(2, $foundRecord3['count']);
     $this->assertSame(1, $foundRecord4['count']);
     $this->assertSame('foo', $foundRecord1['message']);
     $this->assertSame('baz', $foundRecord2['message']);
     $this->assertSame('Error bar', $foundRecord3['message']);
     $this->assertSame('Bad news', $foundRecord3['exception']['message']);
     $this->assertSame('Error bar', $foundRecord4['message']);
     $this->assertSame('Some info', $foundRecord4['exception']['message']);
 }